From 72fcddd507da0b2f21cfa384182273ccdcef152e Mon Sep 17 00:00:00 2001 From: Wojciech Kania Date: Sat, 6 Mar 2021 14:15:39 +0100 Subject: [PATCH] [Validator] Add PHP Attributes to validation page --- validation.rst | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/validation.rst b/validation.rst index b0b74525734..0ad5a3108ec 100644 --- a/validation.rst +++ b/validation.rst @@ -68,6 +68,20 @@ following: private $name; } + .. code-block:: php-attributes + + // src/Entity/Author.php + namespace App\Entity; + + // ... + use Symfony\Component\Validator\Constraints as Assert; + + class Author + { + #[Assert\NotBlank] + private $name; + } + .. code-block:: yaml # config/validator/validation.yaml @@ -351,6 +365,25 @@ literature genre mostly associated with the author, which can be set to either // ... } + .. code-block:: php-attributes + + // src/Entity/Author.php + namespace App\Entity; + + // ... + use Symfony\Component\Validator\Constraints as Assert; + + class Author + { + #[Assert\Choice( + choices: ['fiction', 'non-fiction'], + message: 'Choose a valid genre.', + )] + private $genre; + + // ... + } + .. code-block:: yaml # config/validator/validation.yaml @@ -437,6 +470,22 @@ options can be specified in this way. // ... } + .. code-block:: php-attributes + + // src/Entity/Author.php + namespace App\Entity; + + // ... + use Symfony\Component\Validator\Constraints as Assert; + + class Author + { + #[Assert\Choice(['fiction', 'non-fiction'])] + private $genre; + + // ... + } + .. code-block:: yaml # config/validator/validation.yaml @@ -559,6 +608,20 @@ class to have at least 3 characters. private $firstName; } + .. code-block:: php-attributes + + // src/Entity/Author.php + + // ... + use Symfony\Component\Validator\Constraints as Assert; + + class Author + { + #[Assert\NotBlank] + #[Assert\Length(min: 3)] + private $firstName; + } + .. code-block:: yaml # config/validator/validation.yaml @@ -655,6 +718,23 @@ this method must return ``true``: } } + .. code-block:: php-attributes + + // src/Entity/Author.php + namespace App\Entity; + + // ... + use Symfony\Component\Validator\Constraints as Assert; + + class Author + { + #[Assert\IsTrue(message: 'The password cannot match your first name')] + public function isPasswordSafe() + { + // ... return true or false + } + } + .. code-block:: yaml # config/validator/validation.yaml