0

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

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;
}

然后,在错误中,消息“您的价格不是预期的”与 $form->isValid() 或通过 $this->get('validator') 的产品验证后的 Product 中的属性 $price 相关;

4

1 回答 1

1

您最好编写自定义验证约束。有关说明,请参阅http://symfony.com/doc/current/cookbook/validation/custom_constraint.html

于 2012-05-28T21:59:36.527 回答