Skip to content

Commit 28a7912

Browse files
committed
minor #18335 [Validator] Update the ignoreNull option of UniqueEntity constraint (javiereguiluz)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [Validator] Update the ignoreNull option of UniqueEntity constraint Fixes #18324. Commits ------- a0bb5bf [Validator] Update the ignoreNull option of UniqueEntity constraint
2 parents 1b989b8 + a0bb5bf commit 28a7912

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

reference/constraints/UniqueEntity.rst

+87
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,98 @@ each with a single field.
253253

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

256+
``ignoreNull``
257+
~~~~~~~~~~~~~~
258+
259+
**type**: ``boolean`` | ``string`` | ``array`` **default**: ``true``
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+
In addition to ignoring the ``null`` values of all unique fields, you can also use
267+
this option to specify one or more fields to only ignore ``null`` values on them:
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+
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
278+
use Symfony\Component\Validator\Constraints as Assert;
279+
280+
#[ORM\Entity]
281+
#[UniqueEntity(fields: ['email', 'phoneNumber'], ignoreNull: 'phoneNumber')]
282+
class User
283+
{
284+
// ...
285+
}
286+
287+
.. code-block:: yaml
288+
289+
# config/validator/validation.yaml
290+
App\Entity\User:
291+
constraints:
292+
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
293+
fields: ['email', 'phoneNumber']
294+
ignoreNull: 'phoneNumber'
295+
properties:
296+
# ...
297+
298+
.. code-block:: xml
299+
300+
<!-- config/validator/validation.xml -->
301+
<?xml version="1.0" encoding="UTF-8" ?>
302+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
303+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
304+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
305+
306+
<class name="App\Entity\User">
307+
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
308+
<option name="fields">email</option>
309+
<option name="fields">phoneNumber</option>
310+
<option name="ignore-null">phoneNumber</option>
311+
</constraint>
312+
<!-- ... -->
313+
</class>
314+
</constraint-mapping>
315+
316+
.. code-block:: php
317+
318+
// src/Entity/User.php
319+
namespace App\Entity;
320+
321+
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
322+
use Symfony\Component\Validator\Constraints as Assert;
323+
324+
class User
325+
{
326+
public static function loadValidatorMetadata(ClassMetadata $metadata)
327+
{
328+
$metadata->addConstraint(new UniqueEntity([
329+
'fields' => ['email', 'phoneNumber'],
330+
'ignoreNull' => 'phoneNumber',
331+
]));
332+
333+
// ...
334+
}
335+
}
336+
337+
.. caution::
338+
339+
If you ``ignoreNull`` on fields that are part of a unique index in your
340+
database, you might see insertion errors when your application attempts to
341+
persist entities that the ``UniqueEntity`` constraint considers valid.
342+
343+
.. versionadded:: 6.3
344+
345+
The option to ignore ``null`` values for specific fields was introduced
346+
in Symfony 6.3.
347+
261348
``message``
262349
~~~~~~~~~~~
263350

0 commit comments

Comments
 (0)