symfony-docs
symfony-docs copied to clipboard
[Validator] About Assert alias for constraints
In the documentation for validator constraints, for example NotBlank, constraints are aliased as Assert.
use Symfony\Component\Validator\Constraints as Assert;
#[Assert\NotBlank]
...
I don't really see benefits in this alias, and it adds an indirection (in the code but also in our brain). I find the non-alias version easier to read, understand and maintain.
use Symfony\Component\Validator\Constraints;
#[Constraints\NotBlank]
...
Is there any reason for suggesting adding this alias?
PS: Thanks for your hard work on Symfony!
The namespace is being imported to prevent potentially lots of use
statements for all the constraints that you use. We do the same for Doctrine mapping attributes/annotations.
Thanks for your answer. The shared namespace is really handy and we use it intensively (after teaching Rector not to replace it ;) ). I'm challenging the alias part, aka
use Symfony\Component\Validator\Constraints as Assert;
instead of just
use Symfony\Component\Validator\Constraints;
I'd say the "assert" word really implies something that must fail if it is not true (like the assert()
PHP function). I find it personally "stronger" than "constraint", but I think in any case it's some style preference.
Also, I guess the vast majority of Symfony projects use this (historic?) notation so updating it in the documentation would bring more confusion than anything 😄