Skip to content

[Validator] Add support for the otherwise option in the When constraint #59634

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
Feb 7, 2025

Conversation

alexandre-daubois
Copy link
Member

@alexandre-daubois alexandre-daubois commented Jan 28, 2025

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
Issues Fix #57370
License MIT

This feature allows to give constraints to When when the evaluated expression returns false:

use Symfony\Component\Validator\Constraints\IsNull;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\When;

class BlogPost
{
    public ?int $id = null;
    
    #[When(
        expression: 'this.id !== null', 
        constraints: new Length(min: 10),
        otherwise: new IsNull(),
    )]
    public ?string $content = null;
}

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jan 28, 2025

I'm not sure this is what the issue is about. From what I can read, they need a ternary style instead:

when foo ? then-this : else-that.

#[When('expression', otherwise: new Bar)] would be closer to what's described in the issue.

(please take time to give example in PR descriptions for new feature so that it's easy to bootstrap the doc / write a blog post.)

@alexandre-daubois
Copy link
Member Author

alexandre-daubois commented Jan 28, 2025

I'm not sure this is what the issue is about. From what I can read, they need a ternary style instead

I did so to be consistent with the Expression constraint, taking the last issue comment example as reference which seemed more in line with what already exists. But if we don't care about this point, let's do as you say, works for me as well 👍

(please take time to give example in PR descriptions for new feature so that it's easy to bootstrap the doc / write a blog post.)

My bad indeed, will do!

@nicolas-grekas
Copy link
Member

I don't see the point of a negate option here sorry.

@Kocal
Copy link
Member

Kocal commented Jan 28, 2025

Hey!

Given your proposal, the example from #57370 will become:

#[Assert\When('this.getProduct()?.getNature()?.value === "SESSION"', new Assert\IsNull())]
#[Assert\When('this.getProduct()?.getNature()?.value === "SESSION"', new Assert\NotNull(), negate: true)]

right?

What about this syntax suggested in the issue and by Nicolas above?

#[Assert\When(
    'this.getProduct()?.getNature()?.value === "SESSION"', 
    new Assert\IsNull(), 
    otherwise: new Assert\NotNull()
)]

or maybe a WhenNot assertion?

@alexandre-daubois
Copy link
Member Author

otherwise solution is nice, lets implement this one! I've got one last thing to figure with parent groups propagation in nested constraints and it should be good 👍

@alexandre-daubois alexandre-daubois force-pushed the negate-when branch 2 times, most recently from 7dcfac3 to 5439efb Compare January 28, 2025 19:29
@alexandre-daubois
Copy link
Member Author

It's been more complicated than expected because of how composite constraints work. Their internals has to be updated to accept multiple fields that contains nested constraints (and propagate groups). For this, Composer::getCompositeOption() return type has to be widen to array|string. If I'm right, this does not break BC as child classes are allowed to narrow the return type.

So here's a new implementation, tell me what you think 👍

@alexandre-daubois alexandre-daubois changed the title [Validator] Add support for the negate option in the When constraint [Validator] Add support for the otherwise option in the When constraint Jan 29, 2025
@alexandre-daubois
Copy link
Member Author

alexandre-daubois commented Feb 5, 2025

All comments addressed, thanks!

Should a PR be sent to update to get_debug_type($this) all calls to static::class? There are a lot to them after the deprecation of constraints arguments as array (73 occurrences it seems in Validator)

@nicolas-grekas
Copy link
Member

get_debug_type is to be used in exception messages. that's better, but not critical either.

@fabpot
Copy link
Member

fabpot commented Feb 7, 2025

Thank you @alexandre-daubois.

@fabpot fabpot merged commit bc08cb4 into symfony:7.3 Feb 7, 2025
9 of 11 checks passed
@fabpot fabpot mentioned this pull request May 2, 2025
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.

"When" Constraint -> Possibility to apply validations if expression(s) not matched (same way as if/else)
5 participants