Skip to content
Advertisement

How to set option for validation constraint in Symfony globally?

I need to change format for DateTime validation constraint but I want to do it globally for the whole application to not copy-paste it like this:

JavaScript

(I use yaml config in my application)

Is there any option to do it globally in Symfony 5 application, using DI parameters or some other way?

I didn’t find anything about it in official documentation.

Advertisement

Answer

You can create a custom constraint extending SymfonyComponentValidatorConstraintsDateTime and set the format there.

JavaScript

Then you just use custom constraint where needed.

YAML:

JavaScript

Annotations:

JavaScript

So your config would look like this:

JavaScript

https://symfony.com/doc/current/validation/custom_constraint.html

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement