Skip to content

Commit 61a4867

Browse files
authored
fix: switch custom validation constraint from annotations to attributes (#1677)
1 parent 0e1f0cc commit 61a4867

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

core/validation.md

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ for this task, but you can replace it with your preferred validation library suc
1010

1111
Validating submitted data is as simple as adding [Symfony's built-in constraints](http://symfony.com/doc/current/reference/constraints.html)
1212
or [custom constraints](http://symfony.com/doc/current/validation/custom_constraint.html) directly in classes marked with
13-
the `#[ApiResource]` annotation:
13+
the `#[ApiResource]` attribute:
1414

1515
```php
1616
<?php
@@ -39,9 +39,8 @@ class Product
3939

4040
/**
4141
* @var string[] Describe the product
42-
*
43-
* @MinimalProperties
4442
*/
43+
#[MinimalProperties]
4544
#[ORM\Column(type: 'json')]
4645
public $properties;
4746

@@ -59,9 +58,7 @@ namespace App\Validator\Constraints;
5958

6059
use Symfony\Component\Validator\Constraint;
6160

62-
/**
63-
* @Annotation
64-
*/
61+
#[\Attribute]
6562
class MinimalProperties extends Constraint
6663
{
6764
public $message = 'The product must have the minimal properties required ("description", "price")';
@@ -77,9 +74,6 @@ namespace App\Validator\Constraints;
7774
use Symfony\Component\Validator\Constraint;
7875
use Symfony\Component\Validator\ConstraintValidator;
7976

80-
/**
81-
* @Annotation
82-
*/
8377
final class MinimalPropertiesValidator extends ConstraintValidator
8478
{
8579
public function validate($value, Constraint $constraint): void
@@ -118,7 +112,7 @@ errors to HTTP errors.
118112
Without specific configuration, the default validation group is always used, but this behavior is customizable: the framework
119113
is able to leverage Symfony's [validation groups](https://symfony.com/doc/current/validation/groups.html).
120114

121-
You can configure the groups you want to use when the validation occurs directly through the `ApiResource` annotation:
115+
You can configure the groups you want to use when the validation occurs directly through the `ApiResource` attribute:
122116

123117
```php
124118
<?php
@@ -145,7 +139,7 @@ With the previous configuration, the validation groups `a` and `b` will be used
145139
Like for [serialization groups](serialization.md#using-different-serialization-groups-per-operation),
146140
you can specify validation groups globally or on a per-operation basis.
147141

148-
Of course, you can use XML or YAML configuration format instead of annotations if you prefer.
142+
Of course, you can use XML or YAML configuration format instead of attributes if you prefer.
149143

150144
You may also pass in a [group sequence](http://symfony.com/doc/current/validation/sequence_provider.html) in place of
151145
the array of group names.

0 commit comments

Comments
 (0)