表单不显示模板中的表单错误
对于我的联系表单,没有显示表单错误.
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.