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

Skip to content

[Validator] Add normalizer option to Unique constraint #15124

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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions reference/constraints/Unique.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ Unique
======

Validates that all the elements of the given collection are unique (none of them
is present more than once). Elements are compared strictly, so ``'7'`` and ``7``
are considered different elements (a string and an integer, respectively).
is present more than once). By default elements are compared strictly,
so ``'7'`` and ``7`` are considered different elements (a string and an integer, respectively).
If you want any other comparison logic to be applied, use the `normalizer`_ option.

.. seealso::

Expand All @@ -21,6 +22,7 @@ are considered different elements (a string and an integer, respectively).
Applies to :ref:`property or method <validation-property-target>`
Options - `groups`_
- `message`_
- `normalizer`_
- `payload`_
Class :class:`Symfony\\Component\\Validator\\Constraints\\Unique`
Validator :class:`Symfony\\Component\\Validator\\Constraints\\UniqueValidator`
Expand Down Expand Up @@ -123,4 +125,22 @@ Parameter Description
``{{ value }}`` The current (invalid) value
============================= ================================================

``normalizer``
~~~~~~~~~~~~~~

**type**: a `PHP callable`_ **default**: ``null``

.. versionadded:: 5.3

The ``normalizer`` option was introduced in Symfony 5.3.

This option allows to define the PHP callable applied to each element of the given collection before
checking if the collection is valid.

For example, you may want to pass the ``'trim'`` string to apply the
:phpfunction:`trim` PHP function to each element of the collection in order to ignore leading and trailing
whitespace during validation.

Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a versionadded directive

The ''normalizer'' option was introduced in Symfony 5.3.

' is a backtick, I am currently on a phone 📱😄

.. include:: /reference/constraints/_payload-option.rst.inc

.. _`PHP callable`: https://www.php.net/callable