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

Skip to content

[Validator] Add the When constraint and validator #42593

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
Sep 11, 2022

Conversation

wuchen90
Copy link
Contributor

@wuchen90 wuchen90 commented Aug 16, 2021

Q A
Branch? 6.2
Bug fix? no
New feature? yes
Deprecations? no
License MIT
Doc PR symfony/symfony-docs#15722

This constraint allows you to apply constraints validation only if the provided condition is matched.

Usage:

namespace App\Model;

use Symfony\Component\Validator\Constraints as Assert;

class Discount
{
    private $type; // 'percent' or 'absolute'

    /**
     * @Assert\GreaterThan(0)
     * @Assert\When(
     *     expression="this.type == 'percent'",
     *     constraints={@LessThan(100, message="The value should be between 0 and 100!")}
     * )
     */
    private $value;
    // ...
}

See the documentation for details.

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 5.4 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@wuchen90 wuchen90 force-pushed the conditional-validator branch from 28c2de6 to 965f556 Compare August 17, 2021 09:58
@carsonbot
Copy link

Hey!

I think @lmasforne has recently worked with this code. Maybe they can help review this?

Cheers!

Carsonbot

@wuchen90
Copy link
Contributor Author

Actually there is no Deprecation or Bug in this PR but I don't know how to remove them.

@lyrixx
Copy link
Member

lyrixx commented Aug 18, 2021

How does it play with PHP Attribute?

@wuchen90
Copy link
Contributor Author

How does it play with PHP Attribute?

@lyrixx
I can't find a way to make it work with PHP attribute because the constraints option expects an array of constraint instances as value.
Maybe in future version of PHP attribute it will be permitted.

@nicolas-grekas nicolas-grekas added this to the 5.4 milestone Aug 18, 2021
@nicolas-grekas
Copy link
Member

what about Assert\When(expression="...", ...)?

@derrabus
Copy link
Member

derrabus commented Aug 18, 2021

Maybe in future version of PHP attribute it will be permitted.

PHP 8.1. The future is now. 😉

See also #41994

@Seb33300
Copy link
Contributor

Looks related to #32877

@wuchen90
Copy link
Contributor Author

what about Assert\When(expression="...", ...)?

I don't have a preference for the name.
I would let the community decide on this and I will make the changes.

1️⃣ Assert\Conditional(condition="", ...)
2️⃣ Assert\When(expression="...", ...)
3️⃣ Assert\Condition(condition="", ...)

@wuchen90 wuchen90 force-pushed the conditional-validator branch 4 times, most recently from e5f889f to 523e13f Compare August 19, 2021 13:10
@wuchen90
Copy link
Contributor Author

Maybe in future version of PHP attribute it will be permitted.

PHP 8.1. The future is now. 😉

See also #41994

@derrabus Thanks!

I added PHP attributes support but I don't know how to fix PHPUnit / Tests (8.0) (pull_request) test...

{
if (null === $this->expressionLanguage) {
if (!class_exists(ExpressionLanguage::class)) {
throw new LogicException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
Copy link
Contributor

@julienfalque julienfalque Nov 19, 2021

Choose a reason for hiding this comment

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

Suggested change
throw new LogicException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
throw new LogicException(sprintf('The "symfony/expression-language" component is required to use the "%s" constraint.', __CLASS__));

Maybe move this check to Conditional's constructor like it's done for Expression?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is already the check in the constructor.

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed. This makes this check unnecessary then, because you can't reach this line without an instance of Conditional, which would have already performed the same check via its constructor.

Copy link
Contributor Author

@wuchen90 wuchen90 Dec 1, 2021

Choose a reason for hiding this comment

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

Actually we can reach this line with an instance of a class that extends Conditional and with different constructor.
I get it won't almost never happen but it seems logic in the context of this validator class.
To be honest, I took this check from

if (!class_exists(ExpressionLanguage::class)) {
throw new LogicException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Right. Maybe the constraint class could be made final?

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 don't think I should make it final because it will make the class non extendable for further implementation.

@maxhelias
Copy link
Contributor

Oh I forgot, +1 for Assert\When(expression="...", ...) to be consistent with the dependency injection attribute

@wuchen90 wuchen90 force-pushed the conditional-validator branch 2 times, most recently from 6097bee to 56de60c Compare August 8, 2022 12:17
@wuchen90 wuchen90 changed the title [Validator] Add the Conditional constraint and validator [Validator] Add the When constraint and validator Aug 8, 2022
@wuchen90
Copy link
Contributor Author

wuchen90 commented Aug 8, 2022

@maxhelias
Thanks for the review.
I addressed the changes and did the same for the docs.

@wuchen90 wuchen90 force-pushed the conditional-validator branch 2 times, most recently from 974e2de to e265ad5 Compare August 9, 2022 15:32
Copy link
Member

@xabbuh xabbuh left a comment

Choose a reason for hiding this comment

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

LGTM (except for the minor comment)

@wuchen90 wuchen90 force-pushed the conditional-validator branch from ab07c8b to 421520f Compare August 12, 2022 09:29
@OskarStark OskarStark changed the title [Validator] Add the When constraint and validator [Validator] Add the When constraint and validator Aug 12, 2022
@wuchen90 wuchen90 force-pushed the conditional-validator branch from 421520f to 9b7bdc9 Compare August 12, 2022 14:00
@xabbuh
Copy link
Member

xabbuh commented Sep 11, 2022

Thank you @wuchen90 and congratulations to your first Symfony contribution!

@xabbuh xabbuh merged commit c5d14a6 into symfony:6.2 Sep 11, 2022
@wuchen90 wuchen90 deleted the conditional-validator branch September 11, 2022 21:24
@fabpot fabpot mentioned this pull request Oct 24, 2022
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.