FOS Rest Default Form Error

By Default FOS RestBundle uses jms serializer or the symfony serializer to serialize form errors. These do not conform to our response envelope so we over ride this by over riding a class parameter in the JMS serializer bundle that FOS is using to serialize the form.

services.yml

jms_serializer.form_error_handler.class: AppBundle\Handler\FormErrorHandler

What you will notice if that FOS Rest Bundle over rides the JMS Serializer Bundle. This class is almost copied from the FOS Rest Bundle implementation. The only difference is how we are wrapping the response.

FormErrorHandler

/**
  * This is what controls the response wrapper.
  *
  * @param \ArrayObject $serializedForm
  * @return array
  */
 protected function adaptFormArray(\ArrayObject $serializedForm)
 {
     return [
         'meta' => [
             'type' => 'formErrors',
             'paginated' => false,
         ],
         'data' => $serializedForm,
     ];
 }

Example of json

 {
     "meta": {
         "type": "formErrors",
         "paginated": false
     },
     "data": {
         "children": {
             "email": {
                 "errors": [
                     "This value is already used."
                 ]
             },
             "plainPassword": {
                 "errors": [
                     "This value should not be blank."
                 ]
             }
         }
     }
 }