-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Comments
Thank you for this suggestion. |
Thank you for the reminder, @carsonbot! |
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
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
The text was updated successfully, but these errors were encountered: