@@ -47,6 +47,33 @@ username and the password are different only if all other validation passes
47
47
}
48
48
}
49
49
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
+
50
77
.. code-block :: yaml
51
78
52
79
# config/validator/validation.yaml
@@ -151,7 +178,7 @@ You can also define a group sequence in the ``validation_groups`` form option::
151
178
152
179
// src/Form/MyType.php
153
180
namespace App\Form;
154
-
181
+
155
182
use Symfony\Component\Form\AbstractType;
156
183
use Symfony\Component\OptionsResolver\OptionsResolver;
157
184
use Symfony\Component\Validator\Constraints\GroupSequence;
@@ -204,6 +231,27 @@ entity and a new constraint group called ``Premium``:
204
231
// ...
205
232
}
206
233
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
+
207
255
.. code-block :: yaml
208
256
209
257
# config/validator/validation.yaml
@@ -263,7 +311,7 @@ entity and a new constraint group called ``Premium``:
263
311
{
264
312
$metadata->addPropertyConstraint('name', new Assert\NotBlank());
265
313
$metadata->addPropertyConstraint('creditCard', new Assert\CardScheme([
266
- 'schemes' => [' VISA' ],
314
+ 'schemes' => [Assert\CardScheme:: VISA],
267
315
'groups' => ['Premium'],
268
316
]));
269
317
}
@@ -319,6 +367,19 @@ provides a sequence of groups to be validated:
319
367
// ...
320
368
}
321
369
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
+
322
383
.. code-block :: yaml
323
384
324
385
# config/validator/validation.yaml
0 commit comments