Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Merged
merged 1 commit into from
Jan 21, 2021

Conversation

wkania
Copy link
Contributor

@wkania wkania commented Nov 14, 2020

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.

@wkania
Copy link
Contributor Author

wkania commented Nov 16, 2020

@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?
In my opinion, it should be between the table and Basic Usage header.
The message:

The ability to use PHP attributes to configure constraints was introduced in
Symfony 5.2. Prior to this, Doctrine Annotations were the only way to
annotate field of a class with constraint configuration.

@OskarStark
Copy link
Contributor

OskarStark commented Dec 7, 2020

I would love to get your review here @derrabus 👍 Thanks

@wkania I am not sure if we need this information, the code examples are only available in the 5.2 docs 🤔

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.

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.

Comment on lines 203 to 205
use Symfony\Component\Validator\Constraints as Assert;

#[Assert\Callback(["Acme\Validator", "validate"])]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Contributor Author

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.

Comment on lines 54 to 55
schemes: ["VISA"],
message: "Your credit card number is invalid."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Contributor Author

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.

Comment on lines 75 to 78
/**
* 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.")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* 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.

Copy link
Contributor Author

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


class Author
{
#[Assert\Choice(callback: "getGenres")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[Assert\Choice(callback: "getGenres")]
#[Assert\Choice(callback: 'getGenres')]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


class Author
{
#[Assert\Choice(callback: ["App\Entity\Genre", "getGenres"])]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[Assert\Choice(callback: ["App\Entity\Genre", "getGenres"])]
#[Assert\Choice(callback: [Genre::class, 'getGenres'])]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Comment on lines 62 to 63
minMessage: "You must specify at least one email",
maxMessage: "You cannot specify more than {{ limit }} emails"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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',

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Comment on lines 56 to 57
type: "isbn10",
message : "This value is not valid."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type: "isbn10",
message : "This value is not valid."
type: Assert\Isbn::ISBN_10,
message : 'This value is not valid.',

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Copy link
Contributor Author

@wkania wkania left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@derrabus All fixed

I would love to get your review here @derrabus +1 Thanks

@wkania I am not sure if we need this information, the code examples are only available in the 5.2 docs thinking

I do not know what is the policy behind adding such information, but @derrabus added it.

@derrabus
Copy link
Member

derrabus commented Dec 8, 2020

I don't think that we want to have this versionadded block on each constraint page. On some overview article that describes the various ways a developer might configure constraints, this block is certainly helpful, though.

@wkania
Copy link
Contributor Author

wkania commented Jan 20, 2021

Hi @OskarStark , @derrabus , @xabbuh , @maxhelias
From what I see, @HeahDude is a former member of docs team.
What about his review of this pull request?

@OskarStark
Copy link
Contributor

It took time, but here we go, this is in now. Thank you very much @wkania.

@OskarStark OskarStark merged commit 7b23335 into symfony:5.2 Jan 21, 2021
javiereguiluz added a commit that referenced this pull request Jan 23, 2021
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
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.

6 participants