2012-02-20 40 views

回答

0

是的,你必须建立我们所说的约束,这将被应用到data_class本身,而不是到特定的字段:http://symfony.com/doc/2.0/book/validation.html#classes

+0

这里没有真正回答这个问题。好的,在整个班上应用回调的部分。但是,你如何将错误设置为全局而不是字段? – 2012-10-18 10:14:08

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) }} 
相关问题