I need to have a generic JSON-representation of errors for any form that I have.
I have already read other solutions like in how to return json encoded form errors in symfony. But I don't want to create one more service for task that is already can be solved by other bundle that I connected to my project.
I use JMSSerializerBundle in my project and I know that this bundle can handle Symfony form errors using FormErrorHandler. But now I can get only whole form serialization:
$errors = $form->getErrors();
$serializer = $this->get('jms_serializer');
$json = $serializer->serialize($errors, 'json');
This code will return me next JSON-object:
{
"form": {
"children": {
"field1": [],
"field2": [],
"field_with_error": {
"errors": ["Error text"]
},
"collection": {
"child_form": [
{
"children": {
"field1": [],
"field2": []
}
}
]
}
}
},
"errors": []
}
But I need something like that (only fields with errors):
{
"field_with_error": {
"errors": ["Error text"]
}
}
How can I achieve that? I looked at FOSRestBundle that has already solved this problem. But I didn't find how they did it.
FormErrorHandler). How can I do this?