Skip to content

Commit 5e23027

Browse files
committed
Merge branch '5.2' into 5.x
* 5.2: [Validator] Add PHP Attributes to validation page [Validator] Add PHP Attributes to validation groups
2 parents f8a0636 + f285c25 commit 5e23027

File tree

3 files changed

+164
-2
lines changed

3 files changed

+164
-2
lines changed

validation.rst

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

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)