diff --git a/validation/groups.rst b/validation/groups.rst index 7681f583a08..70dcc975655 100644 --- a/validation/groups.rst +++ b/validation/groups.rst @@ -42,6 +42,27 @@ user registers and when a user updates their contact information later: private $city; } + .. code-block:: php-attributes + + // src/Entity/User.php + namespace App\Entity; + + use Symfony\Component\Security\Core\User\UserInterface; + use Symfony\Component\Validator\Constraints as Assert; + + class User implements UserInterface + { + #[Assert\Email(groups: ['registration'])] + private $email; + + #[Assert\NotBlank(groups: ['registration'])] + #[Assert\Length(min: 7, groups: ['registration'])] + private $password; + + #[Assert\Length(min: 2)] + private $city; + } + .. code-block:: yaml # config/validator/validation.yaml diff --git a/validation/sequence_provider.rst b/validation/sequence_provider.rst index 503c50f67e5..699711b661d 100644 --- a/validation/sequence_provider.rst +++ b/validation/sequence_provider.rst @@ -47,6 +47,33 @@ username and the password are different only if all other validation passes } } + .. code-block:: php-attributes + + // src/Entity/User.php + namespace App\Entity; + + use Symfony\Component\Security\Core\User\UserInterface; + use Symfony\Component\Validator\Constraints as Assert; + + #[Assert\GroupSequence(['User', 'Strict'])] + class User implements UserInterface + { + #[Assert\NotBlank] + private $username; + + #[Assert\NotBlank] + private $password; + + #[Assert\IsTrue( + message: 'The password cannot match your username', + groups: ['Strict'], + )] + public function isPasswordSafe() + { + return ($this->username !== $this->password); + } + } + .. code-block:: yaml # config/validator/validation.yaml @@ -151,7 +178,7 @@ You can also define a group sequence in the ``validation_groups`` form option:: // src/Form/MyType.php namespace App\Form; - + use Symfony\Component\Form\AbstractType; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Validator\Constraints\GroupSequence; @@ -204,6 +231,27 @@ entity and a new constraint group called ``Premium``: // ... } + .. code-block:: php-attributes + + // src/Entity/User.php + namespace App\Entity; + + use Symfony\Component\Validator\Constraints as Assert; + + class User + { + #[Assert\NotBlank] + private $name; + + #[Assert\CardScheme( + schemes: [Assert\CardScheme::VISA], + groups: ['Premium'], + )] + private $creditCard; + + // ... + } + .. code-block:: yaml # config/validator/validation.yaml @@ -263,7 +311,7 @@ entity and a new constraint group called ``Premium``: { $metadata->addPropertyConstraint('name', new Assert\NotBlank()); $metadata->addPropertyConstraint('creditCard', new Assert\CardScheme([ - 'schemes' => ['VISA'], + 'schemes' => [Assert\CardScheme::VISA], 'groups' => ['Premium'], ])); } @@ -319,6 +367,19 @@ provides a sequence of groups to be validated: // ... } + .. code-block:: php-attributes + + // src/Entity/User.php + namespace App\Entity; + + // ... + + #[Assert\GroupSequenceProvider] + class User implements GroupSequenceProviderInterface + { + // ... + } + .. code-block:: yaml # config/validator/validation.yaml