@@ -253,11 +253,103 @@ each with a single field.
253
253
254
254
**type **: ``boolean `` **default **: ``true ``
255
255
256
+ ``ignoreNull ``
257
+ ~~~~~~~~~~~~~~
258
+
259
+ **type **: ``bool ``, ``string `` or ``array `` **default **: ``[] ``
260
+
256
261
If this option is set to ``true ``, then the constraint will allow multiple
257
262
entities to have a ``null `` value for a field without failing validation.
258
263
If set to ``false ``, only one ``null `` value is allowed - if a second entity
259
264
also has a ``null `` value, validation would fail.
260
265
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
+
261
353
``message ``
262
354
~~~~~~~~~~~
263
355
0 commit comments