且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Symfony2 无效形式没有错误

更新时间:2023-11-19 23:00:10

首先要理解的是验证是在模型上完成的,而不是在表单上完成的.表单可以包含错误,但前提是它的字段映射到未验证的属性.因此,如果您的表单不包含无效字段(可能是对不在表单中的属性的 NotNull 断言),则不会显示错误.

The first thing to understand is validation is done on the model, not the form. The form can contain errors, but only if it has a field mapped to the property that doesn't validate. So if your form does not contain the invalid field (maybe a NotNull assertion on a property that is not in the form), it will not show the error.

第二件事是 $form->getErrors() 只会显示该级别的错误,每个表单子项都可以包含自己的错误.所以如果你想检查错误,你应该遍历字段并在每个字段上调用 ​​getErrors .Form 类上的 getErrors 方法可能会以这种方式欺骗.

The second thing is that $form->getErrors() will only show errors for that level, each form child can contain its own errors. So if you want to check the errors, you should loop through the fields and call getErrors on each field. The getErrors method on the Form class can be deceiving that way.