-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Allow creating constraints with required arguments #45072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Validator] Allow creating constraints with required arguments #45072
Conversation
169216f
to
bc1205d
Compare
src/Symfony/Component/Validator/Mapping/Loader/AbstractLoader.php
Outdated
Show resolved
Hide resolved
bc1205d
to
16ef147
Compare
src/Symfony/Component/Validator/Mapping/Loader/AbstractLoader.php
Outdated
Show resolved
Hide resolved
16ef147
to
f72c5c0
Compare
src/Symfony/Component/Validator/Mapping/Loader/AbstractLoader.php
Outdated
Show resolved
Hide resolved
5d6405d
to
63f4d0c
Compare
If I'm not wrong, it's already possible to write constraints with required args, but then they can only be loaded via annotations, and not via yaml/xml, isn't it? Relying on |
yes
And what that |
Exactly that: a marker to express that some constraints opt-in to get their arguments by a named constructor.
The next logical step would be to allow constructing constraints using named arguments, even if no arguments are required. |
Maybe then an attribute could be used for this? because if I'd extend an existing constraint it wouldn't work |
That'd work for me also! |
63f4d0c
to
44c1c1d
Compare
Thank you, updated. |
44c1c1d
to
5aec890
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
almost good to me, I just have some questions
src/Symfony/Component/Validator/Tests/Fixtures/ConstraintWithRequiredArgument.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Mapping/Loader/AbstractLoader.php
Outdated
Show resolved
Hide resolved
5aec890
to
f22433f
Compare
Thank you @norkunas. |
Before PHP8 validation constraint usage was only possible with providing options as an array, but now with native PHP attributes we can provide as named arguments. And to require some arguments overriding
getRequiredOptions
method in Constraint was necessary to get proper validation. But since PHP8.1 we can just make arguments required in the Attribute constructor and try to unpack them because it is possible now.