表单不显示模板中的表单错误 [英] Form does not display form errors in template

查看:36
本文介绍了表单不显示模板中的表单错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的联系表单,没有显示表单错误.

For my contact form no form errors are displayed.

    $contact = new Contact();

    $form = $this->createForm(new ContactFormType(), $contact);

    /* handle contact form submission */
    if ('POST' == $request->getMethod()) {
        $form->bindRequest($request);

        if ($form->isValid()) {
            //Do something
        }
    }

    return $this->render('MainBundle:Default:contact.html.twig', array(
                'form' => $form->createView()
    ));

我的验证器

Fc\MainBundle\Entity\Contact:
    properties:
        firstName:
            - NotBlank: ~

        lastName:
            - NotBlank: ~

        email:
            - NotBlank: ~
            - Email:
                checkMX: true
        title:
            - NotBlank: ~
        message:
            - NotBlank: ~

我在我的表单标签中添加了一个 novalidate 属性来检查我的验证是否有效.现在,当我提交带有空数据的表单时,没有任何反应(使用正确的数据一切正常),控制器将表单识别为无效并停止进一步的处理.

I have added a novalidate attribute to my form tag to check if my validation works. Now when I submit the form with empty data nothing happens (with correct data everything is fine), the controller identifies the form as invalid and stops further processes.

在我调用的模板中

 {{ form_errors(form) }}

但是什么也没有显示.任何想法为什么?我用的是 symfony 2.1.X

But nothing is displayed. Any ideas why? I use symfony 2.1.X

推荐答案

您能否确认验证是否适用于此:

Can you confirm that the validation is working with this:

if ($form->isValid()) {
//Do something
}
else
{ die('not valid') }

表格是否贴好?

HTML 中是否有任何内容显示在您调用 {{ form_errors(form) }} 的位置?- 如果是这种情况,那将是 CSS 或 JS 问题...

Is there anything in the HTML that shows up where you call {{ form_errors(form) }} ? - it would be a CSS or JS problem if that's the case...

您是否尝试过按字段显示错误(显然您的验证中没有全局错误):

Have you tried to show the errors by field (there is no global error in your validation apparently):

{{ form_errors(form.firstName) }}
etc.

备注:form.firstName 有效,而不是 form.first_name.你必须遵循 Doctrine2/Symfony2 的约定.

Remark: form.firstName works, not form.first_name. You have to follow that convention with Doctrine2/Symfony2.

这篇关于表单不显示模板中的表单错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆