You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reimplements the `Valid` constraint in the Symfony validator component as `Cascade`annotation/attribute which is more straightforward to use than `Valid` and has no surprising behavior.
6
+
Reimplements the `Valid` constraint in the Symfony validator component as `Cascade` attribute which is more straightforward to use than `Valid` and has no surprising behavior.
7
7
8
-
This component is compatible with the Symfony validator component in version 5.0+ and will be adapted to support future versions of Symfony (if any changes are necessary for that).
8
+
This component is compatible with the Symfony validator component in version 5.x (v2.x with annotation/attribute support) and 6.x (v3.x with only attribute support) and will be adapted to support future versions of Symfony (if any changes are necessary for that).
9
9
10
10
Installation
11
11
------------
@@ -45,82 +45,6 @@ Below is a common example in real applications: You might have an order and mult
45
45
use Squirrel\ValidatorCascade\Cascade;
46
46
use Symfony\Component\Validator\Constraints as Assert;
47
47
48
-
class Order
49
-
{
50
-
/**
51
-
* Validate $shippingAddress if validation with no validation
52
-
* group or the "Default" validation group is triggered
53
-
*
54
-
* Validates "Default" and "phoneNumberMandatory" validation groups in $shippingAddress
// This validates with the "Default" validation group,
107
-
// so only shippingAddress must be specified
108
-
$symfonyValidator->validate($order);
109
-
110
-
// This also validates the invoice address in addition
111
-
// to the shipping address
112
-
$symfonyValidator->validate($order, null, [
113
-
"Default",
114
-
"alternateInvoiceAddress",
115
-
]);
116
-
```
117
-
118
-
If you are using PHP8+, you should use attributes instead (although annotations will still work):
119
-
120
-
```php
121
-
use Squirrel\ValidatorCascade\Cascade;
122
-
use Symfony\Component\Validator\Constraints as Assert;
123
-
124
48
class Order
125
49
{
126
50
/**
@@ -179,9 +103,7 @@ The current implementation of the `Valid` constraint in the Symfony validator co
179
103
### `Valid` constraint without validation group
180
104
181
105
```php
182
-
/**
183
-
* @Assert\Valid()
184
-
*/
106
+
#[Assert\Valid]
185
107
public $someobject;
186
108
```
187
109
@@ -195,9 +117,7 @@ This is fine for simple objects or when you don't need any validation groups at
195
117
### `Valid` constraint with validation group(s)
196
118
197
119
```php
198
-
/**
199
-
* @Assert\Valid(groups={"invoice"})
200
-
*/
120
+
#[Assert\Valid(groups: ['invoice'])]
201
121
public $someobject;
202
122
```
203
123
@@ -207,6 +127,6 @@ The `Valid` assertion above only triggers when you validate the "invoice" valida
207
127
- There is no way to change which validation groups are triggered in $someobject
208
128
- The "traverse" option for `Valid` is not used when a validation group is defined. Although the "traverse" option should probably not be used or needed in general
209
129
210
-
Having validation groups both as a trigger and as a filter severly limits how you can use it, and makes most use cases (like our [example with addresses](#example)) impossible to do with `Valid`. Even if you manage to make it work, your code will not be self explanatory and it is easy to make mistakes or misunderstand the annotations.
130
+
Having validation groups both as a trigger and as a filter severly limits how you can use it, and makes most use cases (like our [example with addresses](#example)) impossible to do with `Valid`. Even if you manage to make it work, your code will not be self explanatory and it is easy to make mistakes or misunderstand the attributes.
211
131
212
132
`Cascade` as defined in this component separates which validation group the constraint belongs to and which validation groups are triggered in the child object(s). What it cannot do is cascade the validation groups of the parent to the child object, as this information is only available in the `RecursiveContextualValidator` class of the validator component and cannot be accessed without changing a lot of the internals of the validator component (unfortunately).
0 commit comments