当ajax表单无效时,如何在视图中显示表单错误?

时间:2016-03-05 21:53:36

标签: php jquery ajax symfony symfony-forms

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

在ajax请求之后调用操作Controller" updateRoleAction"没问题,我在ajax请求中使用了selrialize来发送数据。当我通过警报显示所有数据时发送。我不明白为什么$editForm->isValid()坚持错误。

AdminController.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:

$(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);
            }});

        });
    }); 

edit.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) }}但仍为空。

1 个答案:

答案 0 :(得分:2)

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

如果您希望在顶级表单中映射错误,则应尝试在表单类型的@RestController public class AjaxHandler { @RequestMapping("/loajax") public String serveAjax(HttpServletRequest request,HttpServletResponse res) { System.out.println("hjhjhjh"); return "loajax"; } } 方法中添加此行:

configureOptions()

要详细了解此选项,请参阅the official documentation