Question

I'm new in Symfony2, so I'm pretty sure this is basic but I can't find the answer on the web,

I have an entity persisted in MySql through Doctrine, all of its properties have their own validation rules in the validation.yml. When the form is for example a Create/Update form, that's fine: if some rule doesn't match, the proper message pops out in the form noticing the user.

However, what happens when inside a method (let's say a setter) I use an object of another class (a utility, not another entity) for a more complex validation that may throw an exception? Is there a way that its message may be shown like the other validation rules in the form?

Was it helpful?

Solution

Instead of throwing an exception, you should use the Callback constraint, to execute the logic inside the utility method just as a validator.

In the documentation referenced above, you can see how you can add error messages to the validation errors from within this utility method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top