Symfony2 form validation in Ajax
ajax, forms, javascript, jquery, symfony
Solution
I created a function myself
public function getFormErrors(Form $form) {
$errors = $form->getErrors();
foreach ($form->all() as $child) {
foreach ($child->getErrors() as $key => $error) {
$template = $error->getMessageTemplate();
$parameters = $error->getMessageParameters();
foreach ($parameters as $var => $value) {
$template = str_replace($var, $value, $template);
}
$errors[$child->getName()][] = $template;
}
}
return $errors;
}
Problem
On certain pages I use forms within bootstrap modals. I submit the form with Ajax and it gets validated in the controller. The most users will fill in the form correctly but if a validation fails the form is re-rendered and got send back to the user. I don't like this at all, but I can't find a better way because I can't get access the validation errors of the fields. Does someone has a better approach to achieve validation errors send back in JSON?