Skip to content

[Validator] Add attributes documentation of composite constraints #15541

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

alexandre-daubois
Copy link
Member

@alexandre-daubois alexandre-daubois commented Jul 20, 2021

Related pull-request: symfony/symfony#41994

Not really sure how to precise it only works for attributes in PHP 8.1, not 8.0. If you have a solution, please let me know!

@alexandre-daubois alexandre-daubois force-pushed the feat-validator-composite-constraints-attribute branch from 2c2bfe1 to 07a39ed Compare July 20, 2021 09:01
@javiereguiluz javiereguiluz added the Waiting Code Merge Docs for features pending to be merged label Jul 20, 2021
@carsonbot carsonbot added this to the next milestone Jul 20, 2021
@javiereguiluz
Copy link
Member

Thanks for this Alexander!

I have two comments:

Nested attribute code syntax

Is there a commonly used syntax for this? I'm asking because instead of this:

    #[
        Assert\All([
            new Assert\NotBlank,
            new Assert\Length(min: 5),
        ])
    ]
    protected $favoriteColors = [];

I'd prefer to use this:

    #[Assert\All([
        new Assert\NotBlank,
        new Assert\Length(min: 5),
    ])]
    protected $favoriteColors = [];

But this is just a personal preference, so let's ping the @symfony/team-symfony-docs to ask for their opinion.

Warning message about needing PHP 8.1 to run this code

That's a good question and I don't have a good answer. Maybe we can add a comment just before each class like the following?

.. code-block:: php-attributes

    // src/Localization/Place.php
    namespace App\Localization;

    use App\Validator\Constraints as AcmeAssert;
    use Symfony\Component\Validator\Constraints as Assert;

    // IMPORTANT: the code of this class requires using PHP 8.1 or higher

    class Place
    {
        // ...
    }

But I'm not sure ... so let's ping @symfony/team-symfony-docs again to ask what do they think. Thanks!

new Assert\Length(min: 10),
])
]
protected $password;
Copy link
Contributor

Choose a reason for hiding this comment

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

We should rename this to plainPassword, in nearly every application, this should be a hash, which does not need to be validated 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right, making the change!

@alexandre-daubois
Copy link
Member Author

Thank you for your feedback @javiereguiluz!

About the code syntax, I have to say that I don't particularly have an opinion either. I based myself on code written by @derrabus, where you can find this in some tests of the framework:

#[
    Assert\NotNull,
    Assert\Range(min: 3),
]
public $firstName;

About the warning message for PHP 8.1, for now I'll add the comment like you did and we'll see if a better solution appears.

@alexandre-daubois alexandre-daubois force-pushed the feat-validator-composite-constraints-attribute branch from 07a39ed to ed56590 Compare July 22, 2021 06:37
fabpot added a commit to symfony/symfony that referenced this pull request Aug 18, 2021
…e-daubois)

This PR was merged into the 5.4 branch.

Discussion
----------

[Validator] Add support of nested attributes

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #38503
| License       | MIT
| Doc PR        | symfony/symfony-docs#15541

Although the RFC (https://wiki.php.net/rfc/new_in_initializers) is in the voting phase (until 14 July), it is already well on its way to passing.

Based on `@nikic`'s development (php/php-src#7153), this makes the development of support possible. It will obviously take a little while before this pull request is merged.

If this pull request is OK for you, I'll get to work on writing the existing documentation for the attribute validation constraints.

![Capture d’écran du 2021-07-05 17-11-23](https://user-images.githubusercontent.com/2144837/124491886-0d2f7d80-ddb4-11eb-8147-493bdc6c48ac.png)

Not sure about the Symfony version to target, as `AtLeastOneOf` has been introduced in 5.1. Although, I couldn't find attributes validation documentation for 4.4.

Commits
-------

1449450 [Validator] Add support of nested attributes for composite constraints
symfony-splitter pushed a commit to symfony/validator that referenced this pull request Aug 18, 2021
…e-daubois)

This PR was merged into the 5.4 branch.

Discussion
----------

[Validator] Add support of nested attributes

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #38503
| License       | MIT
| Doc PR        | symfony/symfony-docs#15541

Although the RFC (https://wiki.php.net/rfc/new_in_initializers) is in the voting phase (until 14 July), it is already well on its way to passing.

Based on `@nikic`'s development (php/php-src#7153), this makes the development of support possible. It will obviously take a little while before this pull request is merged.

If this pull request is OK for you, I'll get to work on writing the existing documentation for the attribute validation constraints.

![Capture d’écran du 2021-07-05 17-11-23](https://user-images.githubusercontent.com/2144837/124491886-0d2f7d80-ddb4-11eb-8147-493bdc6c48ac.png)

Not sure about the Symfony version to target, as `AtLeastOneOf` has been introduced in 5.1. Although, I couldn't find attributes validation documentation for 4.4.

Commits
-------

1449450e2f [Validator] Add support of nested attributes for composite constraints
@malteschlueter
Copy link
Contributor

I think this can be merged now or not?

@wouterj wouterj removed the Waiting Code Merge Docs for features pending to be merged label Jan 14, 2022
@wouterj wouterj force-pushed the feat-validator-composite-constraints-attribute branch from ed56590 to 7099e3f Compare January 14, 2022 14:05
@wouterj wouterj merged commit 441222b into symfony:5.4 Jan 14, 2022
@wouterj
Copy link
Member

wouterj commented Jan 14, 2022

You're right @malteschlueter.

Thank you @alexandre-daubois! I've slightly updated your changes, to use the CS proposed by Javier and to add versionadded directives below the code examples (to emphasize the fact that these attributes are not supported in 5.3, which is different for all other constraint).

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.

7 participants