1

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?

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
leoxmb
  • 29
  • 1
  • 6

1 Answers1

0

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.

Wouter J
  • 41,455
  • 15
  • 107
  • 112