3

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.

0

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

i used $resolver->setDefaults(array('allow_extra_fields' => true )); when i buid the form
Now i can retrieve all error messages but how can i retrieve the "INPUT ID" or the "INPUT NAME" ??
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;
}

Comments

0

for the list of errors in the form you can leave it in an array

   foreach ($form->getErrors(true, false) as $error) {
                $errors[] = $error->current()->getMessage();
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.