symfony2 : Get form errors in controller

Viewed 11061

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.

3 Answers

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();
            }
Related