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
Copy file name to clipboardExpand all lines: README.md
+17-14Lines changed: 17 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,21 +38,23 @@ Displays a warning if some field(s) doesn't have a value. Useful for alerting us
38
38
Uses [`SearchFilter`s][13] to define fields as required conditionally, based on the values of other fields (e.g. only required if `OtherField` has a value greater than 25).
39
39
-**[`RequiredBlocksValidator`][14]**
40
40
Require a specific [elemental block(s)][15] to exist in the `ElementalArea`, with optional minimum and maximum numbers of blocks and optional positional validation.
41
-
-**[`RegexFieldsValidator`][16]**
41
+
-**[`ConstraintsValidator`][16]**
42
+
Validate values against [`symfony/validation` constraints](https://symfony.com/doc/current/reference/constraints.html). This is super powerful - definitely check it out.
43
+
-**[`RegexFieldsValidator`][17]** (deprecated)
42
44
Ensure some field(s) matches a specified regex pattern.
43
45
44
-
### [Abstract Validators][17]
46
+
### [Abstract Validators][18]
45
47
46
-
-**[`BaseValidator`][18]**
48
+
-**[`BaseValidator`][19]**
47
49
Includes methods useful for getting the actual `FormField` and its label.
48
-
-**[`FieldHasValueValidator`][19]**
50
+
-**[`FieldHasValueValidator`][20]**
49
51
Subclass of `BaseValidator`. Useful for validators that require logic to check if a field has any value or not.
50
52
51
-
## [Traits][20]
53
+
## [Traits][21]
52
54
53
-
-**[`ValidatesMultipleFields`][21]**
55
+
-**[`ValidatesMultipleFields`][22]**
54
56
Useful for validators that can be fed an array of field names to be validated.
55
-
-**[`ValidatesMultipleFieldsWithConfig`][22]**
57
+
-**[`ValidatesMultipleFieldsWithConfig`][23]**
56
58
Like `ValidatesMultipleFields` but requires a configuration array for each field to be validated.
57
59
58
60
[0]: docs/en/02-extensions.md
@@ -71,10 +73,11 @@ Like `ValidatesMultipleFields` but requires a configuration array for each field
Copy file name to clipboardExpand all lines: docs/en/01-validators.md
+25Lines changed: 25 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -218,8 +218,33 @@ The `ElementalArea` field holder template doesn't currently render validation er
218
218
219
219
This validator validates when the page (or other `DataObject` that has an `ElementalArea`) is saved or published - but not necessarily when the blocks within the `ElementalArea` are saved or published. This means content authors can work around the validation errors if they really want to.
220
220
221
+
## ConstraintsValidator
222
+
223
+
This validator validates values against `symfony/validation` constraints, providing a wide range of well-tested and varied validation logic with a very simple API.
224
+
225
+
This is the ultimate one-stop-shop for form validation - just about any validation you want can be handled by this validator.
226
+
227
+
```php
228
+
use Symfony\Component\Validator\Constraints\Ip;
229
+
use Symfony\Component\Validator\Constraints\NotBlank;
230
+
231
+
ConstraintsValidator::create([
232
+
// Must be an IP address or blank
233
+
'IpAddress' => [new Ip()],
234
+
// Must be an IP address and explicitly cannot be blank
235
+
'IpAddressRequired' => [new Ip(), new NotBlank()],
236
+
]);
237
+
```
238
+
239
+
See the Symfony [validation constraints reference](https://symfony.com/doc/current/reference/constraints.html) for a list of contraints and their usage.
240
+
241
+
See [validation using `symfony/validator` constraints](https://docs.silverstripe.org/en/developer_guides/model/validation/#symfony-validator) in the Silverstripe CMS documentation for any limitations imposed by Silverstripe CMS itself on this kind of validation.
242
+
221
243
## RegexFieldsValidator
222
244
245
+
> [!WARNING]
246
+
> Deprecated! Use `ConstraintsValidator` with a [`Regex` constraint](https://symfony.com/doc/current/reference/constraints/Regex.html) instead.
247
+
223
248
This validator is used to require field values to match a specific regex pattern. Often it will make sense to have this validation inside a custom `FormField` implementation, but for one-off specific pattern validation of fields that don't warrant their own `FormField` this validator is perfect. It uses (so has all of the functionality and methods of) the [`ValidatesMultipleFieldsWithConfig`](#validatesmultiplefieldswithconfig) trait.
224
249
225
250
Any value that cannot be converted to a string cannot be checked against regex and so is ignored, and therefore implicitly passes validation.
0 commit comments