@@ -68,6 +68,20 @@ following:
6868 private $name;
6969 }
7070
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+
7185 .. code-block :: yaml
7286
7387 # config/validator/validation.yaml
@@ -351,6 +365,25 @@ literature genre mostly associated with the author, which can be set to either
351365 // ...
352366 }
353367
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+
354387 .. code-block :: yaml
355388
356389 # config/validator/validation.yaml
@@ -437,6 +470,22 @@ options can be specified in this way.
437470 // ...
438471 }
439472
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+
440489 .. code-block :: yaml
441490
442491 # config/validator/validation.yaml
@@ -559,6 +608,20 @@ class to have at least 3 characters.
559608 private $firstName;
560609 }
561610
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+
562625 .. code-block :: yaml
563626
564627 # config/validator/validation.yaml
@@ -655,6 +718,23 @@ this method must return ``true``:
655718 }
656719 }
657720
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+
658738 .. code-block :: yaml
659739
660740 # config/validator/validation.yaml
0 commit comments