Skip to content

[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

Conversation

yoannrenard
Copy link
Contributor

Describe how to use PHP attributes when creating custom validation constraints


use Symfony\Component\Validator\Constraint;

#[\Attribute()]
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Member

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.

@derrabus
Copy link
Member

The PR should target 5.2 and we should add a note that attribute constraints have been added with Symfony 5.2.

@yoannrenard yoannrenard changed the base branch from 5.x to 5.2 January 22, 2021 13:40
@yoannrenard yoannrenard force-pushed the using_php_attributes_when_creating_custom_validation_constraint branch from b05f043 to 7212a90 Compare January 22, 2021 13:43
@yoannrenard
Copy link
Contributor Author

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

Copy link
Member

@derrabus derrabus left a 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?

@yoannrenard
Copy link
Contributor Author

@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?

@derrabus
Copy link
Member

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.

@OskarStark OskarStark added this to the 5.2 milestone Jan 27, 2021
@OskarStark OskarStark force-pushed the using_php_attributes_when_creating_custom_validation_constraint branch from 4c1c1f5 to 55c5676 Compare January 27, 2021 08:11
@OskarStark
Copy link
Contributor

Thank you Yoann.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants