8000 Add doc for UniqueEntity new feature · symfony/symfony-docs@c28682c · GitHub
[go: up one dir, main page]

Skip to content

Commit c28682c

Browse files
Add doc for UniqueEntity new feature
1 parent 1b989b8 commit c28682c

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

reference/constraints/UniqueEntity.rst

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,41 @@ each with a single field.
251251
``ignoreNull``
252252
~~~~~~~~~~~~~~
253253

254-
**type**: ``boolean`` **default**: ``true``
254+
**type**: ``boolean`` | ``array`` | ``string`` **default**: ``true``
255255

256256
If this option is set to ``true``, then the constraint will allow multiple
257257
entities to have a ``null`` value for a field without failing validation.
258258
If set to ``false``, only one ``null`` value is allowed - if a second entity
259259
also has a ``null`` value, validation would fail.
260260

261+
As of Symfony 6.3, the ``UniqueEntity`` constraint allows you to ignore null
262+
values on specific fields when checking for uniqueness. This feature can be
263+
helpful when you want to validate the uniqueness of a combination of fields,
264+
but want to exclude combinations where one or more of the fields are null.
265+
266+
.. code-block:: php
267+
268+
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
269+
270+
#[UniqueEntity(
271+
fields: ['name', 'email'],
272+
ignoreNull: 'name',
273+
)]
274+
275+
In the example above, null values for the "name" field will be ignored
276+
when checking for uniqueness.
277+
278+
Similarly, you can ignore null values for multiple fields:
279+
280+
.. code-block:: php
281+
282+
#[UniqueEntity(
283+
fields: ['name', 'country', 'email'],
284+
ignoreNull: ['name', 'country'],
285+
)]
286+
287+
In this case, null values for both the "name" and "country" fields will be ignored.
288+
261289
``message``
262290
~~~~~~~~~~~
263291

0 commit comments

Comments
 (0)
0