Skip to content

[Validator] Add ThisableMessage #53098

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

Open
wants to merge 3 commits into
base: 7.4
Choose a base branch
from

Conversation

emile-prevot-42
Copy link

@emile-prevot-42 emile-prevot-42 commented Dec 16, 2023

Q A
Branch? 7.1
Bug fix? no
New feature? yes
Deprecations? no
License MIT

Hello !
I really like the way you can place assertions on Symfony :

class Person
{
    protected string $firstName;

    #[Assert\EqualTo(
       value: 20,
       message: 'This value should be equal to {{ compared_value }}.'
    )]
    protected int $age;
}

But if our need becomes a little more complex, as when we want to validate several Person at once :

class GroupOfPersons
{
    #[Assert\Valid]
    protected array $persons;
}

If we validate this object with some valid Persons and others not, we'll have a large list of exceptions containing "This value should be equal to 20" with the value path. That's enough to meet many needs, and twig with the form component displays them correctly.

But my need was simply to display "This value should be equal to 20 for the person whose first name is Émile." and I couldn't find any elegant way of doing it.

So I've coded this Assertion which makes it possible to do :

class Person
{
    protected string $firstName;

    
    #[Assert\ThisableMessage([
        new Assert\EqualTo(
           value: 20,
           message: 'This value should be equal to {{ compared_value }} for the person whose first name is {{ this.firstName }}.'
       )
    ])]
    protected int $age;
}

If you find this feature interesting, I'd be happy to write the documentation and the changelog, but I might need a little help to improve the test and source code. And maybe "ThisableMessage" is a name that can evolve.

I use it to validate CSVs containing hundreds of rows over dozens of columns, and adding this assertion had no noticeable effect on performance.

Thank you,

@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 7.1 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

@emile-prevot-42 emile-prevot-42 changed the title First commit ThisableMessageValidator [Validator] Add ThisableMessage Dec 16, 2023
@sstok
Copy link
Contributor

sstok commented Dec 17, 2023

I believe you can already do something a bit more complex using the ExpressionValidator (which is much more advanced than only $this). https://symfony.com/doc/current/reference/constraints/Expression.html or https://symfony.com/doc/current/reference/constraints/Callback.html if you need a truly custom message.

@emile-prevot-42
Copy link
Author

I believe you can already do something a bit more complex using the ExpressionValidator (which is much more advanced than only $this). https://symfony.com/doc/current/reference/constraints/Expression.html or https://symfony.com/doc/current/reference/constraints/Callback.html if you need a truly custom message.

ExpressionValidator does not allow you to customize the error message with {{ this. }}
And CallbackValidator can do the job of any assertion ¯ \ _ (ツ) _ / ¯

@94noni
Copy link
Contributor

94noni commented Dec 18, 2023

what about this one?

@emile-prevot-42
Copy link
Author

what about this one?

Here too, the message remains unchanged.
this is only in expression and not message.

@emile-prevot-42
Copy link
Author

emile-prevot-42 commented Dec 18, 2023

i guess it would be nice if this just worked, without the need for extra ThisableMessage

the feature would be binding this, or crash if used but not bound

It would be nice... but I think it would be a big refactoring job for the team to rethink how violations and their parameters are generated

Because at the moment, all variables implemented as {{ value }} are defined in their validator (IsNullValidator for example).

@emile-prevot-42 emile-prevot-42 force-pushed the feature_thisable_message branch from 02d792b to 1298333 Compare December 18, 2023 17:08
@OskarStark OskarStark changed the title [Validator] Add ThisableMessage [Validator] Add ThisableMessage Dec 29, 2023
@xabbuh xabbuh modified the milestones: 7.1, 7.2 May 15, 2024
@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 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.

7 participants