@@ -327,99 +327,15 @@ literature genre mostly associated with the author, which can be set to either
327
327
{
328
328
// ...
329
329
330
- $metadata->addPropertyConstraint('genre', new Assert\Choice([
331
- ' choices' => ['fiction', 'non-fiction'],
332
- ' message' => 'Choose a valid genre.',
333
- ] ));
330
+ $metadata->addPropertyConstraint('genre', new Assert\Choice(
331
+ choices: ['fiction', 'non-fiction'],
332
+ message: 'Choose a valid genre.',
333
+ ));
334
334
}
335
335
}
336
336
337
337
.. _validation-default-option :
338
338
339
- The options of a constraint can always be passed in as an array. Some constraints,
340
- however, also allow you to pass the value of one, "*default *", option in place
341
- of the array. In the case of the ``Choice `` constraint, the ``choices ``
342
- options can be specified in this way.
343
-
344
- .. configuration-block ::
345
-
346
- .. code-block :: php-attributes
347
-
348
- // src/Entity/Author.php
349
- namespace App\Entity;
350
-
351
- // ...
352
- use Symfony\Component\Validator\Constraints as Assert;
353
-
354
- class Author
355
- {
356
- #[Assert\Choice(['fiction', 'non-fiction'])]
357
- private string $genre;
358
-
359
- // ...
360
- }
361
-
362
- .. code-block :: yaml
363
-
364
- # config/validator/validation.yaml
365
- App\Entity\Author :
366
- properties :
367
- genre :
368
- - Choice : [fiction, non-fiction]
369
- # ...
370
-
371
- .. code-block :: xml
372
-
373
- <!-- config/validator/validation.xml -->
374
- <?xml version =" 1.0" encoding =" UTF-8" ?>
375
- <constraint-mapping xmlns =" http://symfony.com/schema/dic/constraint-mapping"
376
- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
377
- xsi : schemaLocation =" http://symfony.com/schema/dic/constraint-mapping
378
- https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd" >
379
-
380
- <class name =" App\Entity\Author" >
381
- <property name =" genre" >
382
- <constraint name =" Choice" >
383
- <value >fiction</value >
384
- <value >non-fiction</value >
385
- </constraint >
386
- </property >
387
-
388
- <!-- ... -->
389
- </class >
390
- </constraint-mapping >
391
-
392
- .. code-block :: php
393
-
394
- // src/Entity/Author.php
395
- namespace App\Entity;
396
-
397
- // ...
398
- use Symfony\Component\Validator\Constraints as Assert;
399
- use Symfony\Component\Validator\Mapping\ClassMetadata;
400
-
401
- class Author
402
- {
403
- private string $genre;
404
-
405
- public static function loadValidatorMetadata(ClassMetadata $metadata): void
406
- {
407
- // ...
408
-
409
- $metadata->addPropertyConstraint(
410
- 'genre',
411
- new Assert\Choice(['fiction', 'non-fiction'])
412
- );
413
- }
414
- }
415
-
416
- This is purely meant to make the configuration of the most common option of
417
- a constraint shorter and quicker.
418
-
419
- If you're ever unsure of how to specify an option, either check the namespace
420
- ``Symfony\Component\Validator\Constraints `` for the constraint or play it safe
421
- by always passing in an array of options (the first method shown above).
422
-
423
339
Constraints in Form Classes
424
340
---------------------------
425
341
@@ -520,7 +436,7 @@ class to have at least 3 characters.
520
436
$metadata->addPropertyConstraint('firstName', new Assert\NotBlank());
521
437
$metadata->addPropertyConstraint(
522
438
'firstName',
523
- new Assert\Length([' min' => 3] )
439
+ new Assert\Length(min: 3 )
524
440
);
525
441
}
526
442
}
@@ -603,9 +519,9 @@ this method must return ``true``:
603
519
{
604
520
public static function loadValidatorMetadata(ClassMetadata $metadata): void
605
521
{
606
- $metadata->addGetterConstraint('passwordSafe', new Assert\IsTrue([
607
- ' message' => 'The password cannot match your first name',
608
- ] ));
522
+ $metadata->addGetterConstraint('passwordSafe', new Assert\IsTrue(
523
+ message: 'The password cannot match your first name',
524
+ ));
609
525
}
610
526
}
611
527
0 commit comments