@@ -68,6 +68,20 @@ following:
68
68
private $name;
69
69
}
70
70
71
+ .. code-block :: php-attributes
72
+
73
+ // src/Entity/Author.php
74
+ namespace App\Entity;
75
+
76
+ // ...
77
+ use Symfony\Component\Validator\Constraints as Assert;
78
+
79
+ class Author
80
+ {
81
+ #[Assert\NotBlank]
82
+ private $name;
83
+ }
84
+
71
85
.. code-block :: yaml
72
86
73
87
# config/validator/validation.yaml
@@ -351,6 +365,25 @@ literature genre mostly associated with the author, which can be set to either
351
365
// ...
352
366
}
353
367
368
+ .. code-block :: php-attributes
369
+
370
+ // src/Entity/Author.php
371
+ namespace App\Entity;
372
+
373
+ // ...
374
+ use Symfony\Component\Validator\Constraints as Assert;
375
+
376
+ class Author
377
+ {
378
+ #[Assert\Choice(
379
+ choices: ['fiction', 'non-fiction'],
380
+ message: 'Choose a valid genre.',
381
+ )]
382
+ private $genre;
383
+
384
+ // ...
385
+ }
386
+
354
387
.. code-block :: yaml
355
388
356
389
# config/validator/validation.yaml
@@ -437,6 +470,22 @@ options can be specified in this way.
437
470
// ...
438
471
}
439
472
473
+ .. code-block :: php-attributes
474
+
475
+ // src/Entity/Author.php
476
+ namespace App\Entity;
477
+
478
+ // ...
479
+ use Symfony\Component\Validator\Constraints as Assert;
480
+
481
+ class Author
482
+ {
483
+ #[Assert\Choice(['fiction', 'non-fiction'])]
484
+ private $genre;
485
+
486
+ // ...
487
+ }
488
+
440
489
.. code-block :: yaml
441
490
442
491
# config/validator/validation.yaml
@@ -559,6 +608,20 @@ class to have at least 3 characters.
559
608
private $firstName;
560
609
}
561
610
611
+ .. code-block :: php-attributes
612
+
613
+ // src/Entity/Author.php
614
+
615
+ // ...
616
+ use Symfony\Component\Validator\Constraints as Assert;
617
+
618
+ class Author
619
+ {
620
+ #[Assert\NotBlank]
621
+ #[Assert\Length(min: 3)]
622
+ private $firstName;
623
+ }
624
+
562
625
.. code-block :: yaml
563
626
564
627
# config/validator/validation.yaml
@@ -655,6 +718,23 @@ this method must return ``true``:
655
718
}
656
719
}
657
720
721
+ .. code-block :: php-attributes
722
+
723
+ // src/Entity/Author.php
724
+ namespace App\Entity;
725
+
726
+ // ...
727
+ use Symfony\Component\Validator\Constraints as Assert;
728
+
729
+ class Author
730
+ {
731
+ #[Assert\IsTrue(message: 'The password cannot match your first name')]
732
+ public function isPasswordSafe()
733
+ {
734
+ // ... return true or false
735
+ }
736
+ }
737
+
658
738
.. code-block :: yaml
659
739
660
740
# config/validator/validation.yaml
0 commit comments