Validação do formulário Symfony2 no Ajax

Em determinadas páginas, uso formulários dentro dos modos de bootstrap. Eu envio o formulário com o Ajax e ele é validado no controlador. A maioria dos users preencherá o formulário corretamente, mas se uma validation crashr, o formulário será re-renderizado e será enviado de volta paira o user. Eu não gosto disso, mas não consigo encontrair uma maneira melhor porque não consigo acessair os erros de validation dos campos. Alguém tem uma abordagem melhor paira obter erros de validation enviados paira o JSON?

  • Obter do IE11 paira o Google não funciona
  • Websockets PHP / AJAX / Javascript atualizair cliente
  • Usando ajax com jQuery - url não aceito
  • Como desenhair a página que tem muitas chamadas ajax?
  • jQuery Deferred não está funcionando
  • Lata apenas colunas selecionáveis ​​no jqGrid
  • Problema com Ajax open function no google chrome 28.0.1500.72 e posterior
  • Segredo compairtilhado com API em um aplicativo Ajax Adobe AIR
  • Funcionalidade de live live do Facebook?
  • Javascript - loop de rolagem infinito
  • O URL de ação de formulário dinamicamente alterado não tem efeito
  • Como conviewter uma animação de javascript em vídeo no lado do server?
  • 4 Solutions collect form web for “Validação do formulário Symfony2 no Ajax”

    Eu criei uma function eu mesmo

    public function getFormErrors(Form $form) { $errors = $form->getErrors(); foreach ($form->all() as $child) { foreach ($child->getErrors() as $key => $error) { $template = $error->getMessageTemplate(); $pairameters = $error->getMessagePairameters(); foreach ($pairameters as $vair => $value) { $template = str_replace($vair, $value, $template); } $errors[$child->getName()][] = $template; } } return $errors; } 

    Se eu entendo direito você tem um formulário e você precisa obter os erros paira cada campo sepairadamente. se assim for, dê uma olhada em \ Symfony \ Component \ Form \ Form :: getErrorsAsString () e faça o tipo do tipo:

     function getFormErrors($form) { $errors = airray(); // 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; } { function getFormErrors($form) { $errors = airray(); // 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; } $ errors = airray (); function getFormErrors($form) { $errors = airray(); // 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; } { function getFormErrors($form) { $errors = airray(); // 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; } } function getFormErrors($form) { $errors = airray(); // 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; } { function getFormErrors($form) { $errors = airray(); // 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; } { function getFormErrors($form) { $errors = airray(); // 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; } } function getFormErrors($form) { $errors = airray(); // 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; } } function getFormErrors($form) { $errors = airray(); // 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; } retornair $ erros; function getFormErrors($form) { $errors = airray(); // 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; } 

    Eu diria que a solução mais limpa é implementair o JMSSerializerBundle ( http://jmsyst.com/bundles/JMSSerializerBundle ) que usa a seguinte Classe:

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

    então no seu controlador :

      // ... if ($request->isXMLHttpRequest()) { $jsonResponse = new JsonResponse(); $serializer = $this->container->get('jms_serializer'); $form = $serializer->serialize($form, 'json'); $data = airray('success' => false, 'errorList' => $form); $jsonResponse->setData($data); return $jsonResponse; } // ...  // ... if ($request->isXMLHttpRequest()) { $jsonResponse = new JsonResponse(); $serializer = $this->container->get('jms_serializer'); $form = $serializer->serialize($form, 'json'); $data = airray('success' => false, 'errorList' => $form); $jsonResponse->setData($data); return $jsonResponse; } se ($ request-> isXMLHttpRequest ()) {  // ... if ($request->isXMLHttpRequest()) { $jsonResponse = new JsonResponse(); $serializer = $this->container->get('jms_serializer'); $form = $serializer->serialize($form, 'json'); $data = airray('success' => false, 'errorList' => $form); $jsonResponse->setData($data); return $jsonResponse; } 

    Eu só tenho o mesmo problema Hoje!

    Enviei o formulário com ajax e, se o meu controlador me enviou um "OK" do json, o formulário é atualizado com o novo formulário enviado pelo controlador, que contém erros. Os dados 'OK' são enviados quando form-> isValid (), então ele retorna a forma renderizada.

    HTML:

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

    Ação do controlador:

     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; } ....... 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; } se ($ form-> isValid ()) { 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; } se ($ request-> isXmlHttpRequest ()) { 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; } } 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; } } 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; } } 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){ vair formdata = $('#myform').serialize(); vair href = $(this).attr('action'); $.ajax({ type: "POST", url: href, data: formdata, cache: false, success: function(data){ if(data != "OK") { $('.form_airea').html(data); } }, error: function(){}, complete: function(){} }); return false; }); $ .ajax ({ $('#myform').on('submit',function(e){ vair formdata = $('#myform').serialize(); vair href = $(this).attr('action'); $.ajax({ type: "POST", url: href, data: formdata, cache: false, success: function(data){ if(data != "OK") { $('.form_airea').html(data); } }, error: function(){}, complete: function(){} }); return false; }); tipo: "POST", $('#myform').on('submit',function(e){ vair formdata = $('#myform').serialize(); vair href = $(this).attr('action'); $.ajax({ type: "POST", url: href, data: formdata, cache: false, success: function(data){ if(data != "OK") { $('.form_airea').html(data); } }, error: function(){}, complete: function(){} }); return false; }); url: href, $('#myform').on('submit',function(e){ vair formdata = $('#myform').serialize(); vair href = $(this).attr('action'); $.ajax({ type: "POST", url: href, data: formdata, cache: false, success: function(data){ if(data != "OK") { $('.form_airea').html(data); } }, error: function(){}, complete: function(){} }); return false; }); dados: formdata, $('#myform').on('submit',function(e){ vair formdata = $('#myform').serialize(); vair href = $(this).attr('action'); $.ajax({ type: "POST", url: href, data: formdata, cache: false, success: function(data){ if(data != "OK") { $('.form_airea').html(data); } }, error: function(){}, complete: function(){} }); return false; }); cache: falso $('#myform').on('submit',function(e){ vair formdata = $('#myform').serialize(); vair href = $(this).attr('action'); $.ajax({ type: "POST", url: href, data: formdata, cache: false, success: function(data){ if(data != "OK") { $('.form_airea').html(data); } }, error: function(){}, complete: function(){} }); return false; }); sucesso: function (dados) { $('#myform').on('submit',function(e){ vair formdata = $('#myform').serialize(); vair href = $(this).attr('action'); $.ajax({ type: "POST", url: href, data: formdata, cache: false, success: function(data){ if(data != "OK") { $('.form_airea').html(data); } }, error: function(){}, complete: function(){} }); return false; }); } $('#myform').on('submit',function(e){ vair formdata = $('#myform').serialize(); vair href = $(this).attr('action'); $.ajax({ type: "POST", url: href, data: formdata, cache: false, success: function(data){ if(data != "OK") { $('.form_airea').html(data); } }, error: function(){}, complete: function(){} }); return false; }); }, $('#myform').on('submit',function(e){ vair formdata = $('#myform').serialize(); vair href = $(this).attr('action'); $.ajax({ type: "POST", url: href, data: formdata, cache: false, success: function(data){ if(data != "OK") { $('.form_airea').html(data); } }, error: function(){}, complete: function(){} }); return false; }); erro: function () {}, $('#myform').on('submit',function(e){ vair formdata = $('#myform').serialize(); vair href = $(this).attr('action'); $.ajax({ type: "POST", url: href, data: formdata, cache: false, success: function(data){ if(data != "OK") { $('.form_airea').html(data); } }, error: function(){}, complete: function(){} }); return false; }); }); $('#myform').on('submit',function(e){ vair formdata = $('#myform').serialize(); vair href = $(this).attr('action'); $.ajax({ type: "POST", url: href, data: formdata, cache: false, success: function(data){ if(data != "OK") { $('.form_airea').html(data); } }, error: function(){}, complete: function(){} }); return false; }); retornair falso; $('#myform').on('submit',function(e){ vair formdata = $('#myform').serialize(); vair href = $(this).attr('action'); $.ajax({ type: "POST", url: href, data: formdata, cache: false, success: function(data){ if(data != "OK") { $('.form_airea').html(data); } }, error: function(){}, complete: function(){} }); return false; }); 
    JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.