Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between symfony2 callbacks and custom validation constraints

As far as I understand a callback is a constraint that you can customise and set to any field for any type of validation.

A custom validation constraint overrides the base constraint class (creating any type of validation on any field)

I'm just not sure what the difference is, why would I use one and not the other? Are there any performance differences too?

like image 553
Andrew Atkinson Avatar asked Mar 04 '13 15:03

Andrew Atkinson


1 Answers

I haven't researched the Form Component that much to be aware of any performance differences, but besides that, why you should choose one over the other:

Callbacks

  • It is meant to customize the whole validation process, not just the Constraint. For instance, you can set where the error needs to be displayed;
  • The target is always a class, you can't use it on a property;
  • You can't reuse it, it is only available on that class/entity.

Custom Validator Constraints

  • You can reuse it everywhere (as said by @MrGlass, you can even use services as constraint);
  • It can be used on a class and property target;
  • You can only customize when something fails, not what is done after it fails.
like image 145
Wouter J Avatar answered Nov 02 '22 23:11

Wouter J