-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Validator] Use PHP attributes when creating custom validation constraints #14865
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] Use PHP attributes when creating custom validation constraints #14865
Conversation
validation/custom_constraint.rst
Outdated
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
#[\Attribute()] |
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.
@derrabus do we recommend a leading slash and the brackets?
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.
@OskarStark
I got rid of the brackets like it is in the php annotations snippets.
Though the leading slash looks required to me as it doesn't work without.
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.
@OskarStark Symfony's CS policy is that we don't import classes from the global namespace, so the leading backslash is correct.
The PR should target 5.2 and we should add a note that attribute constraints have been added with Symfony 5.2. |
b05f043
to
7212a90
Compare
I've added a small note inspired by the one made for creating routes as attributes as you requested @derrabus and changed the PR target to 5.2 |
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.
Looks good. Shall we add a section explaining how to enable a constraint for both usages (Doctrine Annotations + PHP Attributes)? Or is this a problem, an application usually does not have?
@derrabus You mean something explaining how to use both (Doctrine annotations + PHP attributes) at the same time? Like using a constraint that supports attributes and one which doesn't? |
You can build classes that are a valid Doctrine annotation as well as a valid PHP attribute. Almost all of Symfony's built-in constraints work that way. The examples you provided only work with one of the annotation implementations. But that's probably knowledge most developers won't need. I think, we're good here. |
4c1c1f5
to
55c5676
Compare
Thank you Yoann. |
Describe how to use PHP attributes when creating custom validation constraints