2012-05-28 63 views
0

我有一个“产品”实体,我想用自定义回调函数验证此类的属性(例如价格)。 我的自定义验证比sf2(minLength,max等)提供的默认验证更复杂。我愿做这样的事情:Symfony2自定义验证

class Product 
{ 
/** 
* @Assert\NotBlank() 
* @Assert\CallbackValidationFunction('validatePrice', 'Your price is not the expected') 
*/ 
private $price; 
} 

function validatePrice($priceValue){ 
$x = " i want"; 
return $priceValue == "the value".$x; 
} 

然后,在错误“你们的价格预期”的消息的任意不等阶属性$价格产品的$形式 - > isValid()的或产品相关的后验证通过$ this-> get('validator');

+0

你读过这个页面:http://symfony.com/doc/current/reference/constraints/Callback.html? – greg0ire

+0

恩,谢谢,这似乎对我有用......我会尝试实施解决方案 – Oriam

回答