Skip to content

Commit c487502

Browse files
committed
[Validator] Update the ignoreNull option of UniqueEntity constraint
1 parent 1b989b8 commit c487502

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

reference/constraints/UniqueEntity.rst

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,103 @@ each with a single field.
253253

254254
**type**: ``boolean`` **default**: ``true``
255255

256+
``ignoreNull``
257+
~~~~~~~~~~~~~~
258+
259+
**type**: ``bool``, ``string`` or ``array`` **default**: ``[]``
260+
256261
If this option is set to ``true``, then the constraint will allow multiple
257262
entities to have a ``null`` value for a field without failing validation.
258263
If set to ``false``, only one ``null`` value is allowed - if a second entity
259264
also has a ``null`` value, validation would fail.
260265

266+
Instead of ignoring all or none of the ``null`` values, you can also set this
267+
option to the name of the property or set of properties to ignore their ``null`` values:
268+
269+
.. configuration-block::
270+
271+
.. code-block:: php-attributes
272+
273+
// src/Entity/User.php
274+
namespace App\Entity;
275+
276+
use Doctrine\ORM\Mapping as ORM;
277+
278+
// DON'T forget the following use statement!!!
279+
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
280+
281+
use Symfony\Component\Validator\Constraints as Assert;
282+
283+
#[ORM\Entity]
284+
#[UniqueEntity(fields: ['email', 'phoneNumber'], ignoreNull: 'phoneNumber')]
285+
class User
286+
{
287+
// ...
288+
}
289+
290+
.. code-block:: yaml
291+
292+
# config/validator/validation.yaml
293+
App\Entity\User:
294+
constraints:
295+
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
296+
fields: ['email', 'phoneNumber']
297+
ignoreNull: 'phoneNumber'
298+
properties:
299+
# ...
300+
301+
.. code-block:: xml
302+
303+
<!-- config/validator/validation.xml -->
304+
<?xml version="1.0" encoding="UTF-8" ?>
305+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
306+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
307+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
308+
309+
<class name="App\Entity\User">
310+
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
311+
<option name="fields">email</option>
312+
<option name="fields">phoneNumber</option>
313+
<option name="ignore-null">phoneNumber</option>
314+
</constraint>
315+
<!-- ... -->
316+
</class>
317+
</constraint-mapping>
318+
319+
.. code-block:: php
320+
321+
// src/Entity/User.php
322+
namespace App\Entity;
323+
324+
// DON'T forget the following use statement!!!
325+
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
326+
327+
use Symfony\Component\Validator\Constraints as Assert;
328+
329+
class User
330+
{
331+
public static function loadValidatorMetadata(ClassMetadata $metadata)
332+
{
333+
$metadata->addConstraint(new UniqueEntity([
334+
'fields' => ['email', 'phoneNumber'],
335+
'ignoreNull' => 'phoneNumber',
336+
]));
337+
338+
// ...
339+
}
340+
}
341+
342+
.. caution::
343+
344+
If you ``ignoreNull`` on fields that are part of a unique index in your
345+
database, you might see insertion errors when your application attempts to
346+
persist entities that the ``UniqueEntity`` constraint considers valid.
347+
348+
.. versionadded:: 6.3
349+
350+
The option to ignore ``null`` values for specific properties was introduced
351+
in Symfony 6.3.
352+
261353
``message``
262354
~~~~~~~~~~~
263355

0 commit comments

Comments
 (0)