1
votes

Got a question about how to properly output form submission errors in Symfony2 when they come back from an ajax response.

I am posting a form via ajax, if the form is not filled in correctly, it will send back a reponse with the errors using the following code...

 $errors = $form->getErrorsAsString();
 $return = array("responseCode"=>200, "responseVal"=>"Error", "errorReport"=>$errors);

This will create an array of errors and other variables like so:

 {"responseCode":200,"responseVal":"Error","errorReport":"ERROR: Name cannot be blank.\nERROR: Address cannot be blank.\nERROR: City cannot be blank.\nERROR: State cannot be blank.\nERROR: Zip cannot be blank.\nERROR: Phone cannot be blank.\nERROR: Email cannot be blank.\nname:\n    No errors\naddress:\n    No errors\ncity:\n    No errors\nstate:\n    No errors\nzip:\n    No errors\nemail:\n    No errors\nfax:\n    No errors\nphone:\n    No errors\n"}

I am then using jQuery to write the errors to a div, like so:

 $("#errorReport").html(data.errorReport);

This gives me a div with the following content:

 ERROR: Name cannot be blank. ERROR: Address cannot be blank. ERROR: City cannot be blank. ERROR: State cannot be blank. ERROR: Zip cannot be blank. ERROR: Phone cannot be blank. ERROR: Email cannot be blank. name: No errors address: No errors city: No errors state: No errors zip: No errors email: No errors fax: No errors phone: No errors

This looks really tacky. Is there anyway in Twig or Symfony that I can format these errors so that it looks presentable when they are passed back to the twig template? I'd like it to look like this, but I just don't know how its done:

 Name cannot be blank. 
 Address cannot be blank. 
 City cannot be blank. 
 State cannot be blank. 
 Zip cannot be blank. 
 Phone cannot be blank. 
 Email cannot be blank. 

 (any of the "No errors" would not be shown)

Thanks so much for your help!!!

2
how about $errors = strtr($form->getErrorsAsString(), array('\n' => '<br />')); ?Adam Elsodaney

2 Answers

5
votes

You should use the $form->getErrors() method instead of $form->getErrorsAsString(); getErrors function Returns FormError Object which can be used to create your error messages

So the code would look something like this

$errors = $form->getErrors();
$errorCollection = array();
foreach($errors as $error){
       $errorCollection[] = $error->getMessageTemplate()
}
$return = array("responseCode"=>200, "responseVal"=>"Error", "errorReport"=>$errorCollection);
0
votes

I'd say that the cleanest solution is to implement JMSSerializerBundle (http://jmsyst.com/bundles/JMSSerializerBundle) which uses the following Class:

https://github.com/schmittjoh/serializer/blob/6bfebdcb21eb0e1eb04aa87a68e0b706193b1e2b/src/JMS/Serializer/Handler/FormErrorHandler.php

then in your controller:

        // ...
        if ($request->isXMLHttpRequest()) {
        $jsonResponse = new JsonResponse();

        $serializer = $this->container->get('jms_serializer');
        $form = $serializer->serialize($form, 'json');

        $data = array('success' => false,
                       'errorList' => $form);

        $jsonResponse->setData($data);

        return $jsonResponse;
    }