FOSRestBundle: Detailed form errors

At the moment the form errors look something like:

{
  "code": 400,
  "message": "Validation Failed",
  "errors": {
    "children": {
      "username": {
        "errors": [
          "This value should not be blank."
        ]
      }
    }
  }
}

I would like to have both error message and error code for each validated form field, so the json would look like this example below http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api#errors

{
  "code" : 1024,
  "message" : "Validation Failed",
  "errors" : [
    {
      "code" : 5432,
      "field" : "first_name",
      "message" : "First name cannot have fancy characters"
    },
    {
       "code" : 5622,
       "field" : "password",
       "message" : "Password cannot be blank"
    }
  ]
}

Is it possible to obtain this with Symfony? Is it possible to associate an error code to a validation constraint? Or do you see any other solution?

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 1
  • Comments: 22 (11 by maintainers)

Most upvoted comments

@eXtreme

I had similar issue and solved it with ExceptionWrapperHandler:

class ExceptionWrapperHandler implements ExceptionWrapperHandlerInterface
{

    /**
     * @param array $data
     *
     * @return ExceptionWrapper
     */
    public function wrap($data)
    {
        if ($data['errors'] instanceof Form) {
            $form = $data['errors'];
            $data['errors'] = [];
            foreach ($form->getErrors(true, true) as $error) {
                $path = $error->getCause()->getPropertyPath();
                $data['errors'][$path] = $error->getMessage();
            }
        }
        return new ExceptionWrapper($data);
    }

}

In my Controller I simply return Form if it’s not valid:

class UserController extends FOSRestController
{

    /**
     * @Post("/register")
     *
     * @param Request $request
     * @return array
     */
    public function registerAction(Request $request)
    {
        $form = $this->container->get('fos_user.registration.form');
        /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
        $userManager = $this->container->get('fos_user.user_manager');

        $user = $userManager->createUser();
        $form->setData($user);

        $form->handleRequest($request);

        if (!$form->isValid()) {
            return $form;
        }

        $user->setEnabled(true);
        $userManager->updateUser($user);

        return $user;
    }

}

Example result:

{
  "code": 400,
  "message": "Validation Failed",
  "errors": {
    "data.username": "Ta nazwa u\u017cytkownika jest ju\u017c zaj\u0119ta",
    "data.email": "Podany email jest zaj\u0119ty"
  }
}

@lucascourot no, I don’t know. I never tried to customize the serialization of errors to add such error code, so I don’t have a solution for you

For mobile application form errors is a big trouble, because have a different formats (hash or array) 😦