当 ajax 窗体无效时,如何在视图中显示窗体错误


How to display form errors in a view when an ajax form is invalid?

我正在尝试使用 Ajax 方法提交表单,但我没有收到错误消息,表单无效。

在 ajax 请求后,行动调用控制器"updateRoleAction"是可以的,我在 ajax 请求中使用了 selrialize 来发送数据帖子。 当我通过警报显示此消息时,所有数据都已发送。 我不明白为什么$editForm->isValid()坚持错误。

管理控制器的一部分.php

 public function updateRoleAction(Request $request, $id)
    {
        if ($this->container->get('request')->isXmlHttpRequest()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository('BISSAPUserBundle:Role')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Topic entity.');
            }
                $editForm = $this->createEditForm($entity);
                $editForm->handleRequest($request);
            if ($editForm->isValid()) {
                    $em->persist($entity); 
                    $em->flush();
                $entities_role = $em->getRepository('BISSAPUserBundle:Role')->findAll();
                return $this->render('BISSAPAdminForumBundle:Admin:role.html.twig', array(
            'entities_role' => $entities_role, 'entity' => $entity,
                ));
            }
            return $this->render('BISSAPAdminForumBundle:Role:edit.html.twig', array(
                'entity'      => $entity,
                'edit_form'   => $editForm->createView(),
                'error_output' => "No post"
            ));
        }
    }

Ajax jquery on submit :

$(function(){
        $(document).on('submit', "#form_edit_A", function(e) {
            e.preventDefault();
            var id = $("#bissap_userbundle_role_submit").data('id');
            var scheme = "http://localhost";
            var route = scheme.concat("/bodykoncept/web/app_dev.php/admin/admin/role/").concat(id).concat("/update");
            var el = $('#td_role');
            var $this = $(this);
            alert($this.serialize());
            $.ajax({type: 'POST', dataType: 'html', data: $this.serialize(), url: route, success: function(response){
                el.html(response);
            }, error: function(jqXHR, textStatus, errorThrown) {
                console.log(jqXHR.status);
                console.log(textStatus);
                console.log(errorThrown);
            }});
        });
    }); 

编辑.html.twig:

{% block body -%}
<form name="bissap_userbundle_role" method="post" action="/bodykoncept/web/app_dev.php/admin/admin/role/" id="form_edit_A" role="form" class="tr selected">
<div class="td col-md-1"> 
                {{ form_widget(edit_form._token) }}
                {{ edit_form.vars.value.id }}
</div>
<div class="td col-md-3"> 
                {{ form_widget(edit_form.role) }}
</div>
<div class="td col-md-3"> 
                {{ form_widget(edit_form.description) }}
</div>
<div class="td col-md-3"> 
                {{ form_widget(edit_form.groupRole) }}
</div>
<div class="td col-md-3"> 
                <button type="submit" form="form_edit_A" id="bissap_userbundle_role_submit" name="bissap_userbundle_role[submit]" class="update_class btn" data-id="2">Update</button>
</div>
</form>
<div>{{ form_errors(edit_form) }}</div>
<div>{{ error_output}}</div>

{% endblock %}

在这里,我尝试显示错误消息{{ form_errors(edit_form) }}表单,但仍然是空的。

这可能是因为错误附加到其相应的字段。

如果您希望将错误映射到顶级表单中,则应尝试在表单类型的configureOptions()方法中添加此行:

// src/AppBundle/Form/Type/EditForm.php
namespace AppBundle'Form'Type;
use Symfony'Component'Form'AbstractType;
use Symfony'Component'OptionsResolver'OptionsResolver;
class EditFormType extends AbstractType
{
    // ...
    public function configureOptions(OptionsResolver $resolver)
    {
        // ...
        $resolver->addDefault('error_bubbling', true);
    }
    // ...
}

要了解有关此选项的更多信息,请参阅官方文档。