Skip to content

Commit 00a2289

Browse files
committed
minor #15064 [Validator] Add PHP Attributes to validation groups (wkania)
This PR was merged into the 5.2 branch. Discussion ---------- [Validator] Add PHP Attributes to validation groups <!-- If your pull request fixes a BUG, use the oldest maintained branch that contains the bug (see https://symfony.com/releases for the list of maintained branches). If your pull request documents a NEW FEATURE, use the same Symfony branch where the feature was introduced (and `5.x` for features of unreleased versions). --> Commits ------- 8a2e8d7 [Validator] Add PHP Attributes to validation groups
2 parents cb6e7a8 + 8a2e8d7 commit 00a2289

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

validation/groups.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@ user registers and when a user updates their contact information later:
4242
private $city;
4343
}
4444
45+
.. code-block:: php-attributes
46+
47+
// src/Entity/User.php
48+
namespace App\Entity;
49+
50+
use Symfony\Component\Security\Core\User\UserInterface;
51+
use Symfony\Component\Validator\Constraints as Assert;
52+
53+
class User implements UserInterface
54+
{
55+
#[Assert\Email(groups: ['registration'])]
56+
private $email;
57+
58+
#[Assert\NotBlank(groups: ['registration'])]
59+
#[Assert\Length(min: 7, groups: ['registration'])]
60+
private $password;
61+
62+
#[Assert\Length(min: 2)]
63+
private $city;
64+
}
65+
4566
.. code-block:: yaml
4667
4768
# config/validator/validation.yaml

validation/sequence_provider.rst

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,33 @@ username and the password are different only if all other validation passes
4747
}
4848
}
4949
50+
.. code-block:: php-attributes
51+
52+
// src/Entity/User.php
53+
namespace App\Entity;
54+
55+
use Symfony\Component\Security\Core\User\UserInterface;
56+
use Symfony\Component\Validator\Constraints as Assert;
57+
58+
#[Assert\GroupSequence(['User', 'Strict'])]
59+
class User implements UserInterface
60+
{
61+
#[Assert\NotBlank]
62+
private $username;
63+
64+
#[Assert\NotBlank]
65+
private $password;
66+
67+
#[Assert\IsTrue(
68+
message: 'The password cannot match your username',
69+
groups: ['Strict'],
70+
)]
71+
public function isPasswordSafe()
72+
{
73+
return ($this->username !== $this->password);
74+
}
75+
}
76+
5077
.. code-block:: yaml
5178
5279
# config/validator/validation.yaml
@@ -151,7 +178,7 @@ You can also define a group sequence in the ``validation_groups`` form option::
151178

152179
// src/Form/MyType.php
153180
namespace App\Form;
154-
181+
155182
use Symfony\Component\Form\AbstractType;
156183
use Symfony\Component\OptionsResolver\OptionsResolver;
157184
use Symfony\Component\Validator\Constraints\GroupSequence;
@@ -204,6 +231,27 @@ entity and a new constraint group called ``Premium``:
204231
// ...
205232
}
206233
234+
.. code-block:: php-attributes
235+
236+
// src/Entity/User.php
237+
namespace App\Entity;
238+
239+
use Symfony\Component\Validator\Constraints as Assert;
240+
241+
class User
242+
{
243+
#[Assert\NotBlank]
244+
private $name;
245+
246+
#[Assert\CardScheme(
247+
schemes: [Assert\CardScheme::VISA],
248+
groups: ['Premium'],
249+
)]
250+
private $creditCard;
251+
252+
// ...
253+
}
254+
207255
.. code-block:: yaml
208256
209257
# config/validator/validation.yaml
@@ -263,7 +311,7 @@ entity and a new constraint group called ``Premium``:
263311
{
264312
$metadata->addPropertyConstraint('name', new Assert\NotBlank());
265313
$metadata->addPropertyConstraint('creditCard', new Assert\CardScheme([
266-
'schemes' => ['VISA'],
314+
'schemes' => [Assert\CardScheme::VISA],
267315
'groups' => ['Premium'],
268316
]));
269317
}
@@ -319,6 +367,19 @@ provides a sequence of groups to be validated:
319367
// ...
320368
}
321369
370+
.. code-block:: php-attributes
371+
372+
// src/Entity/User.php
373+
namespace App\Entity;
374+
375+
// ...
376+
377+
#[Assert\GroupSequenceProvider]
378+
class User implements GroupSequenceProviderInterface
379+
{
380+
// ...
381+
}
382+
322383
.. code-block:: yaml
323384
324385
# config/validator/validation.yaml

0 commit comments

Comments
 (0)