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

Skip to content

[Validator] Unique Validator possible improvement #37451

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

Closed
henry2778 opened this issue Jun 29, 2020 · 2 comments
Closed

[Validator] Unique Validator possible improvement #37451

henry2778 opened this issue Jun 29, 2020 · 2 comments

Comments

@henry2778
Copy link
Contributor

Description
I have a suggestion regarding the Unique Validator - i.e the comparison of elements. The thing that I have faced, is that when validating the collection of objects we often want to check the uniqueness not in the 'in array strict' sense, but more business logic sense. For example, I would like to check that my Category has Collection of unique Books. Then the Unique Validator will pass, because I have physically different objects there. But what is needed - is to check if they have different Titles for example. So maybe it would better if we could apply some context to the comparison.

For example, we could pass some callable in constraint, that will be applied to each element of iterable (basically we can call it normalizer). This can give us more flexibility not only for comparing objects, but other types too. That normalizer is trivial by default of course - so no BC breaks

WDYT? The reason opened that issue is that we had to create basically the same validator as UniqueValidator, with 90% same codebase in 2 projects already :)

I would like to work on that if that makes sense

Example

Tweaking https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator/Constraints/UniqueValidator.php#L42
as follows

        $normalizer = $constraint->normalizer;
        foreach ($value as $element) {
           
            $element = $normalizer($element);

            if (\in_array($element, $collectionElements, true)) {
                $this->context->buildViolation($constraint->message)
                    ->setParameter('{{ value }}', $this->formatValue($value))
                    ->setCode(Unique::IS_NOT_UNIQUE)
                    ->addViolation();

                return;
            }
            $collectionElements[] = $element;
        }
@carsonbot
Copy link

Thank you for this suggestion.
There has not been a lot of activity here for a while. Would you still like to see this feature?

@derrabus
Copy link
Member

Thank you for the reminder, @carsonbot!

@carsonbot carsonbot removed the Stalled label Feb 21, 2021
@fabpot fabpot closed this as completed Mar 17, 2021
derrabus added a commit that referenced this issue Mar 18, 2021
… (henry2778)

This PR was squashed before being merged into the 5.3-dev branch.

Discussion
----------

[Validator] Add normalizer option to Unique constraint

| Q             | A
| ------------- | ---
| Branch?       | 5.x
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | #37451
| License       | MIT
| Doc PR        | todo

Hello! Reopening my PR #37507 with target branch `5.x`

This PR is about #37451. The idea is to make that constraint more flexible and able to process business rules, especially when working with objects. We can think about it as the similar feature in UniqueEntity constraint, when we declare on which attributes we are applying the constraint. But in our case it is more general - we pass a callable with whatever logic we want to apply to collection elements before we apply 'uniqueness check' :)

Looks like no BC breaks. Thanks! :)

Commits
-------

44e1e8b [Validator] Add normalizer option to Unique constraint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants