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; });
source share