Symfony2 form validation in Ajax

On some pages, I use forms in bootstrap models. I submit the form using Ajax and is validated in the controller. Most users fill out the form correctly, but if the check fails, the form is re-displayed and sent back to the user. I don’t like this at all, but I can’t find a better way, because I can’t access the field validation errors. Does anyone have a better approach for getting validation errors sent back to JSON?

+6
source share
4 answers

I created the 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; } 
+3
source

if I understand correctly that you have a form, and you need to get errors for each field separately. if so, take a look at \ Symfony \ Component \ Form \ Form :: getErrorsAsString () and do something like:

 function getFormErrors($form) { $errors = array(); // get the form errors foreach($form->getErrors() as $err) { // check if form is a root if($form->isRoot()) $errors['__GLOBAL__'][] = $err->getMessage(); else $errors[] = $err->getMessage(); } // check if form has any children if($form->count() > 0) { // get errors from form child foreach ($form->getIterator() as $key => $child) { if($child_err = getFormErrors($child)) $errors[$key] = $child_err; } } return $errors; } 
+2
source

I would say that the cleanest solution is to implement the 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; } 
+2
source

I have the same problem today!

I submitted the form using ajax, and if my controller did not send me json 'OK', the form will update with the new form submitted by the controller, which contains errors. The 'OK' data is sent when form-> isValid (), otherwise it returns a visualization of the form.

HTML:

 <div class="form_area"> <form id="myform" action.... > ...code form ... </form> </div> 

Controller action:

 use Symfony\Component\HttpFoundation\JsonResponse; public function myEditAction(){ ....... if ( $request->getMethod() == 'POST' ) { $form->bind($request); if ($form->isValid()) { ... code whn valide ... if ( $request->isXmlHttpRequest() ) { return new JsonResponse('OK'); } } } return $form; } 

JS:

 $('#myform').on('submit',function(e){ var formdata = $('#myform').serialize(); var href = $(this).attr('action'); $.ajax({ type: "POST", url: href, data: formdata, cache: false, success: function(data){ if(data != "OK") { $('.form_area').html(data); } }, error: function(){}, complete: function(){} }); return false; }); 
+1
source

Source: https://habr.com/ru/post/959326/


All Articles