I use Symfony2 forms to validate POST and PUT API requests. The form processes the binding of the request data to the main object and then validates the object. Everything works very well, except for collecting errors. I use FOSRestBundle and throw a Symfony \ Component \ HttpKernel \ Exception \ HttpException with a status code of 400 and a message containing form error messages if validation fails. FOSRestBundle handles the conversion of this to a JSON response. The controller method that I have to execute is as follows (all fields fill in their errors before the form):
protected function validateEntity(AbstractType $type, $entity, Request $request)
{
$form = $this->createForm($type, $entity);
$form->bind($request);
if (! $form->isValid()) {
$message = ['Invalid parameters passed.'];
foreach ($form->getErrors() as $error) {
$message[] = $error->getMessage();
}
throw new HttpException(Codes::HTTP_BAD_REQUEST, implode("\n", $message));
}
}
, $form- > getErrors(), , , . , POST PUT . , " ", . :