-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Validator] Document constraints as php 8 Attributes #14544
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
@javiereguiluz Hi. Before I will add info about the attributes in constraints, could I receive some review about the message and position of the message?
|
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.
Thank you very much for working on the documentation of my feature.
The added code blocks look pretty much correct. However, I'd like to suggest a couple of code-style changes:
- Some constraints have worked with magic strings in the past (
CardScheme
,Isbn
). While implementing the attributes I have introduced constants for those strings and my suggestion would be to use them. - We should use
::class
instead of FQCN strings imho, but I don't know if there's a policy for the docs regarding that preference. - Symfony's code-style prefers single quotes over double quotes. I'd suggest to apply this preference to attributes as well. They're PHP code, after all.
- I'd prefer to use trailing commas in multiline attributes. This would be consistent with Symfony's code style for multiline arrays and multiline Doctrine-style annotations.
reference/constraints/Callback.rst
Outdated
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
#[Assert\Callback(["Acme\Validator", "validate"])] |
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.
use Symfony\Component\Validator\Constraints as Assert; | |
#[Assert\Callback(["Acme\Validator", "validate"])] | |
use Acme\Validator; | |
use Symfony\Component\Validator\Constraints as Assert; | |
#[Assert\Callback([Validator::class, 'validate'])] |
- We should prefer
::class
over FQCN strings in PHP code. - We should use single quotes if we don't need the additional semantics of double quotes.
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.
Ok, good to know. I have created attributes examples based on the annotation. I will fix it everywhere. In some other pull request, I will also update the annotations.
reference/constraints/CardScheme.rst
Outdated
schemes: ["VISA"], | ||
message: "Your credit card number is invalid." |
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.
schemes: ["VISA"], | |
message: "Your credit card number is invalid." | |
schemes: [Assert\CardScheme::VISA], | |
message: 'Your credit card number is invalid.', |
- We have added constants for the available card schemes and I suggest we make use of them.
- CS: prefer single quotes.
- CS: use trailing comma in multiline constructor calls.
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.
I added commas in all multiline constructor calls.
reference/constraints/Choice.rst
Outdated
/** | ||
* You can also directly provide an array constant to the "choices" option in the annotation | ||
*/ | ||
#[Assert\Choice(choices: Conference::GENRES, message: "Choose a valid genre.")] |
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.
/** | |
* You can also directly provide an array constant to the "choices" option in the annotation | |
*/ | |
#[Assert\Choice(choices: Conference::GENRES, message: "Choose a valid genre.")] | |
// You can also directly provide an array constant to the "choices" option in the annotation. | |
#[Assert\Choice(choices: Author::GENRES, message: 'Choose a valid genre.')] |
The code comment explains the following attribute block. I find it confusing to use a phpdoc block for that.
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.
Just copy, paste from annotation example. Fixed
reference/constraints/Choice.rst
Outdated
|
||
class Author | ||
{ | ||
#[Assert\Choice(callback: "getGenres")] |
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.
#[Assert\Choice(callback: "getGenres")] | |
#[Assert\Choice(callback: 'getGenres')] |
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.
Fixed
reference/constraints/Choice.rst
Outdated
|
||
class Author | ||
{ | ||
#[Assert\Choice(callback: ["App\Entity\Genre", "getGenres"])] |
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.
#[Assert\Choice(callback: ["App\Entity\Genre", "getGenres"])] | |
#[Assert\Choice(callback: [Genre::class, 'getGenres'])] |
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.
Fixed
reference/constraints/Count.rst
Outdated
minMessage: "You must specify at least one email", | ||
maxMessage: "You cannot specify more than {{ limit }} emails" |
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.
minMessage: "You must specify at least one email", | |
maxMessage: "You cannot specify more than {{ limit }} emails" | |
minMessage: 'You must specify at least one email', | |
maxMessage: 'You cannot specify more than {{ limit }} emails', |
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.
Fixed
reference/constraints/Isbn.rst
Outdated
type: "isbn10", | ||
message : "This value is not valid." |
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.
type: "isbn10", | |
message : "This value is not valid." | |
type: Assert\Isbn::ISBN_10, | |
message : 'This value is not valid.', |
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.
Fixed
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.
I don't think that we want to have this |
Hi @OskarStark , @derrabus , @xabbuh , @maxhelias |
bd52960
to
748bd54
Compare
It took time, but here we go, this is in now. Thank you very much @wkania. |
This PR was submitted for the 5.x branch but it was merged into the 5.2 branch instead. Discussion ---------- [Validator] Use constant instead of magic string Related to the [comment](#14544 (review)). Commits ------- 9db655a [Validator] Use constant instead of magic string
We already have examples of code for the PHP attributes.
We also have Constraints as php 8 Attributes and 2.
The rest will be implemented in the future.