【发布时间】:2012-11-25 21:32:27
【问题描述】:
我有一个Entity,我使用验证annotations 装饰了它,如下所示:
use Symfony\Component\Validator\Constraints as Assert;
class Entity
{
/**
* @Assert\MaxLength(100)
*/
protected $property;
...
}
在$property 的设置器中,我想知道提交表单时注释的验证是否成功。如果验证成功(或不成功),我将在 PHP 中执行其他无法通过注释获得的操作。
这可能吗?即:
...
function setProperty($value)
{
if(annotation_validation_passed_when_form_submitted)
{
$value = do_something($value);
}
$this->property = $value;
}
...
【问题讨论】:
-
你用的是什么版本?
-
Symfony 2.1.3 - 教义 2
-
您必须使验证器可作为类属性或
setProperty()的参数访问,我认为这很混乱。也许编写一个在$form->isValid()上触发错误的自定义 ValidationConstraint 会更有帮助?
标签: validation symfony annotations doctrine entity