Oriam
Oriam

Reputation: 641

Symfony2 custom validation

I have a "product" entity and i want to validate a property(for example price) of this class with a custom callback function. My custom validation is more complex than the defaults validation provided by sf2(minLength, max, etc). I wish to do something like this:

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

then, in the errors the message 'Your price is not the expected' is related withthe property $price in Product after a $form->isValid() or a product validation via $this->get('validator');

Upvotes: 0

Views: 782

Answers (1)

Chris McKinnel
Chris McKinnel

Reputation: 15082

You'd be better off writing a custom validation constraint. See http://symfony.com/doc/current/cookbook/validation/custom_constraint.html for instructions.

Upvotes: 1

Related Questions