【问题标题】:Symfony2 custom validationSymfony2 自定义验证
【发布时间】:2012-05-28 20:40:59
【问题描述】:

我有一个“产品”实体,我想使用自定义回调函数验证此类的属性(例如价格)。 我的自定义验证比 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 相关;

【问题讨论】:

标签: validation symfony entity


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    • 2012-03-01
    • 2013-11-15
    • 2013-07-14
    相关资源
    最近更新 更多