symfony2 : Get form errors in controller
10,668
Solution 1
Maybe Your for myte is builded from multiple nested types? Try to use getErrors(true)
http://symfony.com/doc/current/components/form/introduction.html#accessing-form-errors
Solution 2
/**
* get Error Messages From Form.
* @param \Symfony\Component\Form\Form $form
* @return array
*/
protected function getErrorMessages(\Symfony\Component\Form\Form $form) {
$errors = array();
if ($form->count() > 0) {
foreach ($form->all() as $child) {
if (!$child->isValid()) {
$errors[$child->getName()] = (String) $form[$child->getName()]->getErrors();
}
}
}
return $errors;
}
Author by
WebMozarts
I write applications using technologies you haven't even heard of. My code is poetry, meanwhile yours is oh-noetry.
Updated on June 30, 2022Comments
-
WebMozarts almost 2 years
I validated my form through AJAX CALL but can't retrieve "error messages".
Here is what i put :
$newRdvForm = $this->createForm(new RdvType()); $newRdvForm->handleRequest($request); if ($newRdvForm->isValid()) { // set of instructions to be performed when the form is valid } else { $errors= array(); foreach ($newRdvForm->getErrors() as $key => $error) { $errors[$key] = $error->getMessage(); } $response = new Response(json_encode($errors)); $response->headers->set('Content-Type', 'application/json'); return $response; }
I used symfony 2.7. The above code doesn't retrieve the error messages.
-
WebMozarts about 8 yearsi used $resolver->setDefaults(array('allow_extra_fields' => true )); when i buid the form
-
WebMozarts about 8 yearsNow i can retrieve all error messages but how can i retrieve the "INPUT ID" or the "INPUT NAME" ??