0

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');

2

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.