我可以使用回调验证器设置一般表单错误吗?

时间:2012-02-20 13:55:25

标签: symfony

我可以使用回调验证器设置常规表单错误吗?我不想将其设置为特定字段,而是将其设置为一般错误的表单。

2 个答案:

答案 0 :(得分:0)

是的,您必须创建我们称之为的约束,该约束将应用于data_class本身,而不是特定字段:http://symfony.com/doc/2.0/book/validation.html#classes

答案 1 :(得分:0)

以下是一段代码,用于将错误设置为全局表单而不是字段。

public function isValid(ExecutionContext $context) {

    if (what ever condition) {

        // Do not set the property path as shown in the documentation
        $context->addViolation('This name sounds totally fake!', array(), null);
    }
}

如果未在上下文中定义属性路径,则会在表单的顶级添加违规。您所要做的就是在官方文档中删除这两行

 $propertyPath = $context->getPropertyPath() . '.firstName';
 $context->setPropertyPath($propertyPath);

然后简单地显示表单的全局错误。

{{ form_errors(form) }}