Skip to content

Commit 1014fcc

Browse files
Merge pull request #11 from GuySartorelli/cms6
CMS 6 compatibility.
2 parents a20b959 + 01c0eba commit 1014fcc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+226
-1343
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,16 @@ jobs:
1313
permissions:
1414
pull-requests: read
1515
contents: read
16+
actions: write
1617
uses: silverstripe/gha-ci/.github/workflows/ci.yml@v1
17-
permissions:
18-
pull-requests: read
19-
contents: write
2018
with:
2119
extra_jobs: |
22-
- php: 8.1
20+
- php: 8.3
2321
phpunit: false
2422
endtoend: true
2523
endtoend_suite: admin
2624
endtoend_config: vendor/silverstripe/admin/behat.yml
27-
- php: 8.2
25+
- php: 8.4
2826
phpunit: false
2927
endtoend: true
3028
endtoend_suite: cms

README.md

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,12 @@ composer require guysartorelli/silverstripe-composable-validators
2222

2323
The `AjaxCompositeValidator` adds a submit handler to your form. This doesn't always interact well with other submit handlers, and can result in either front-end validation being skipped or the form not submitting the way you expect it to, depending on which submit handler gets the event first. For best results, don't add additional submit handlers to the form.
2424

25-
If you're using the `AjaxCompositeValidator` on a form that uses [undefinedoffset/silverstripe-nocaptcha][3] 2.3.0 or higher, you should disable form submission handling for the `NocaptchaField` in that form (see instructions in the nocaptcha docs).
25+
If you're using the `AjaxCompositeValidator` on a form that uses [undefinedoffset/silverstripe-nocaptcha][3], you should disable form submission handling for the `NocaptchaField` in that form (see instructions in the nocaptcha docs).
2626

2727
## [Available Validators][4]
2828

2929
- **[`AjaxCompositeValidator`][5]**
3030
Subclass of [`CompositeValidator`][6] that provides AJAX validation. Resolves [an issue with losing data][7], faster turn-around for fixing validation problems, and provides a way to use the same validation for 'client-side' validation of frontend forms.
31-
- **[`SimpleFieldsValidator`][8]**
32-
Ensures the internal validation of form fields by calling `validate()` on them.
33-
- **[`RequiredFieldsValidator`][9]**
34-
Like Silverstripe's [`RequiredFields`][10] validator, but more convenient for use in a `CompositeValidator`.
3531
- **[`WarningFieldsValidator`][11]**
3632
Displays a warning if some field(s) doesn't have a value. Useful for alerting users about data that is technically valid but may not provide the results they expect
3733
- **[`DependentRequiredFieldsValidator`][12]**
@@ -40,8 +36,6 @@ Uses [`SearchFilter`s][13] to define fields as required conditionally, based on
4036
Require a specific [elemental block(s)][15] to exist in the `ElementalArea`, with optional minimum and maximum numbers of blocks and optional positional validation.
4137
- **[`ConstraintsValidator`][16]**
4238
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)
44-
Ensure some field(s) matches a specified regex pattern.
4539

4640
### [Abstract Validators][18]
4741

@@ -63,18 +57,14 @@ Like `ValidatesMultipleFields` but requires a configuration array for each field
6357
[3]: https://github.com/UndefinedOffset/silverstripe-nocaptcha
6458
[4]: docs/en/01-validators.md
6559
[5]: docs/en/01-validators.md#ajaxcompositevalidator
66-
[6]: https://api.silverstripe.org/4/SilverStripe/Forms/CompositeValidator.html
60+
[6]: https://api.silverstripe.org/6/SilverStripe/Forms/Validation/CompositeValidator.html
6761
[7]: https://github.com/silverstripe/silverstripe-elemental/issues/764
68-
[8]: docs/en/01-validators.md#simplefieldsvalidator
69-
[9]: docs/en/01-validators.md#requiredfieldsvalidator
70-
[10]: https://api.silverstripe.org/4/SilverStripe/Forms/RequiredFields.html
7162
[11]: docs/en/01-validators.md#warningfieldsvalidator
7263
[12]: docs/en/01-validators.md#dependentrequiredfieldsvalidator
7364
[13]: https://docs.silverstripe.org/en/developer_guides/model/searchfilters/
7465
[14]: docs/en/01-validators.md#requiredblocksvalidator
7566
[15]: https://github.com/silverstripe/silverstripe-elemental
7667
[16]: docs/en/01-validators.md#constraintsvalidator
77-
[17]: docs/en/01-validators.md#regexfieldsvalidator
7868
[18]: docs/en/01-validators.md#abstract-validators
7969
[19]: docs/en/01-validators.md#basevalidator
8070
[20]: docs/en/01-validators.md#fieldhasvaluevalidator

_config/config.yml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
---
22
Name: guysartorelli-composable-validators-config
33
---
4-
# Don't validate the NocaptchaField during AJAX validation as it can only be validated once.
5-
Signify\ComposableValidators\Validators\SimpleFieldsValidator:
6-
ignore_field_classes_on_ajax:
7-
- UndefinedOffset\NoCaptcha\Forms\NocaptchaField
8-
94
# Add styling to CMS
105
SilverStripe\Admin\LeftAndMain:
116
extra_requirements_css:
127
- 'guysartorelli/silverstripe-composable-validators:client/dist/left-and-main.css'
13-
14-
# Replace new FieldsValidator with our SimpleFieldsValidator
15-
SilverStripe\Core\Injector\Injector:
16-
SilverStripe\Forms\FieldsValidator:
17-
class: Signify\ComposableValidators\Validators\SimpleFieldsValidator

_config/extensions.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ SilverStripe\Forms\Form:
55
extensions:
66
- Signify\ComposableValidators\Extensions\FormExtension
77

8-
SilverStripe\Forms\FormField:
8+
SilverStripe\Admin\LeftAndMain:
99
extensions:
10-
- Signify\ComposableValidators\Extensions\FormFieldExtension
10+
- Signify\ComposableValidators\Extensions\LeftAndMainAjaxValidationExtension

composer.json

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"keywords": [
88
"silverstripe",
99
"composable",
10-
"composite",
1110
"CompositeValidator",
1211
"validator",
1312
"validators",
@@ -19,7 +18,6 @@
1918
"frontend",
2019
"dependent",
2120
"required",
22-
"regex",
2321
"warning",
2422
"elemental"
2523
],
@@ -37,15 +35,15 @@
3735
"issues": "https://github.com/GuySartorelli/silverstripe-composable-validators/issues"
3836
},
3937
"require": {
40-
"php": "^8.1",
41-
"silverstripe/framework": "^5.2"
38+
"php": "^8.3",
39+
"silverstripe/framework": "^6"
4240
},
4341
"require-dev": {
44-
"silverstripe/cms": "^5",
45-
"silverstripe/asset-admin": "^2",
46-
"dnadesign/silverstripe-elemental": "^5",
47-
"silverstripe/frameworktest": "^1",
48-
"silverstripe/recipe-testing": "^3"
42+
"silverstripe/cms": "^6",
43+
"silverstripe/asset-admin": "^3",
44+
"dnadesign/silverstripe-elemental": "^6",
45+
"silverstripe/frameworktest": "^2",
46+
"silverstripe/recipe-testing": "^4"
4947
},
5048
"extra": {
5149
"expose": [

docs/en/01-validators.md

Lines changed: 18 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,28 @@ All of these validators can be used in the CMS _and_ in the front-end.
66

77
## AjaxCompositeValidator
88

9-
**Important:** See the [extensions docs](./02-extensions.md) for extensions that are highly recommended if you intend to use this validator.
9+
> [!IMPORTANT]
10+
> See the [extensions docs](./02-extensions.md) for extensions that are highly recommended if you intend to use this validator.
1011
1112
Note that to use this validator on the frontend, you will need to expose `jQuery` as a global variable. To avoid providing outdated or redundant copies of jQuery this module doesn't come packaged with it.
1213

13-
As of Silverstripe 4.7.0, all `DataObject`s have a [`CompositeValidator`](https://api.silverstripe.org/4/SilverStripe/Forms/CompositeValidator.html) automatically for CMS forms. The `AjaxCompositeValidator` is a subclass of that validator and provides AJAX validation that takes affect prior to form submission. When you click a form action that isn't validation exempt, an AJAX request is made to validate the form _prior_ to form submission. If there are any validation errors, form submission will be blocked and validation messages displayed.
14+
All `DataObject`s have a [`CompositeValidator`](https://api.silverstripe.org/6/SilverStripe/Forms/Validation/CompositeValidator.html) automatically for CMS forms. The `AjaxCompositeValidator` is a subclass of that validator and provides AJAX validation that takes affect prior to form submission. When you click a form action that isn't validation exempt, an AJAX request is made to validate the form _prior_ to form submission. If there are any validation errors, form submission will be blocked and validation messages displayed.
1415

15-
This is useful for situations where data can be lost with the normal Silverstripe validation pipeline (e.g. validating fields on a page that has an [elemental area](https://github.com/silverstripe/silverstripe-elemental) - see [this issue](https://github.com/silverstripe/silverstripe-elemental/issues/764)), and is also faster as it doesn't need to reload the form after validating to display the error messages.
16+
This is typically faster as it doesn't need to reload and re-render the form after validating to display the error messages.
1617

1718
This validator is also extremely useful for front-end forms, as it provides client-side-esque validation without having to re-write all of your validation logic. It even checks for Google Recaptcha v2, and will produce an error if it identifies that a Recaptcha v2 exists for the form but was not completed.
1819

1920
### Usage
2021

21-
If the [`DataObjectDefaultAjaxExtension`](./02-extensions.md#dataobjectdefaultajaxextension) extension has been applied, calling `parent::getCMSCompositeValidator()` inside the `getCMSCompositeValidator()` method will return an `AjaxCompositeValidator`, which can then be manipulated.
22+
> [!HINT]
23+
> See the [optional configuration](./02-extensions.md) for information about replacing the default `CompositeValidator` with `AjaxCompositeValidator`.
2224
23-
You can also opt to just return a new `AjaxCompositeValidator` from that method - and in front-end situations, you can instantiate a new `AjaxCompositeValidator` (preferably [via injection](https://docs.silverstripe.org/en/developer_guides/extending/injector/) i.e. `AjaxCompositeValidator::create()`).
25+
You can also opt to just return a new `AjaxCompositeValidator` from `getCMSCompositeValidator()` - and in front-end situations, you can instantiate a new `AjaxCompositeValidator` (preferably [via injection](https://docs.silverstripe.org/en/developer_guides/extending/injector/) i.e. `AjaxCompositeValidator::create()`).
2426

2527
Ajax validation can also be disabled at any stage, if there is a cause for doing so.
2628

2729
```PHP
28-
// This example assumes use of the validator in the CMS, with the DataObjectDefaultAjaxExtension extension applied.
29-
// If this was for frontend use, you would be well-advised to explicitly include a SimpleFieldsValidator.
30+
// This example assumes use of the validator in the CMS, with the optional configuration applied.
3031
public function getCMSCompositeValidator(): CompositeValidator
3132
{
3233
$validator = parent::getCMSCompositeValidator();
@@ -48,74 +49,29 @@ public function getCMSCompositeValidator(): CompositeValidator
4849
}
4950
```
5051

51-
### Known Issues
52-
53-
Some actions (such as delete, archive, and restore) should be allowed even if the data is not valid (we should be allowed to delete an object _especially_ if its data is invalid), but those actions are not validation exempt by default. [Two extensions](./02-extensions.md#dataobjectvalidationexemptionextension-and-gridfielditemrequestvalidationexemptionextension) are provided with this module to remedy this and we strongly recommend applying them.
52+
### Exclude fields from AJAX validation
5453

55-
## SimpleFieldsValidator
54+
You may also want to omit certain `FormField` subclasses from validation during AJAX validation calls, and only validate them during the final form submission.
5655

57-
This validator simply calls validate on all fields in the form, ensuring the internal validation of form fields. It should _always_ be included in an `AjaxCompositeValidator` unless some other validator being used also performs that function (such as Silverstripe's own `RequiredFields` validator - though you should generally use this module's `RequiredFieldsValidator` instead).
58-
59-
### Usage
56+
This can be useful if (as in the case of the [undefinedoffset/silverstripe-nocaptcha](https://github.com/UndefinedOffset/silverstripe-nocaptcha) module's `NocaptchaField`) the field cannot be validated more than once with the same value.
6057

61-
In most situations this validator will require no configuration at all.
62-
63-
However, this validator comes with [an extension](02-extensions.md#formfieldextension) which adds `setOmitFieldValidation()` and `getOmitFieldValidation()` methods to all `FormField`s. This can be used if, for specific use cases, internal field validation should be conditional. In that case you can set `OmitFieldValidation` to true, and handle the conditional validation of the field in a separate validator.
64-
65-
```PHP
66-
// In the DataObject which needs the custom validation
67-
public function getCMSFields()
68-
{
69-
$fields = parent::getCMSFields();
70-
$fields->add(SomeField::create('FieldName')->setOmitFieldValidation(true));
71-
return $fields;
72-
}
73-
public function getCMSCompositeValidator() : CompositeValidator
74-
{
75-
$validator = parent::getCMSCompositeValidator();
76-
$validator->addValidator(CustomValidator::create());
77-
return $validator;
78-
}
79-
80-
// In your CustomValidator
81-
public function php($data)
82-
{
83-
$valid = true;
84-
/* Other custom validation here */
85-
if ($fieldNeedsValidation) {
86-
$someField = $this->form->Fields()->dataFieldByName('FieldName');
87-
$valid = $someField->validate($this) && $valid;
88-
}
89-
return $valid;
90-
}
91-
```
92-
93-
You may also want to omit certain `FormField` subclasses from validation during AJAX validation calls (assuming you're using the `AjaxCompositeValidator`), and only validate them during the final form submission. This can be useful if (as in the case of the [undefinedoffset/silverstripe-nocaptcha](https://github.com/UndefinedOffset/silverstripe-nocaptcha) module's `NocaptchaField`) the field cannot be validated more than once with the same value.
94-
95-
You can do this by setting the class name for that `FormField` in the `SimpleFieldsValidator`'s `ignore_field_classes_on_ajax` config array.
58+
You can do this by setting the class name for that `FormField` in the `AjaxCompositeValidator`'s `remove_before_ajax_validation` config array.
9659

9760
```yml
98-
Signify\ComposableValidators\Validators\SimpleFieldsValidator:
99-
ignore_field_classes_on_ajax:
61+
Signify\ComposableValidators\Validators\AjaxCompositeValidator:
62+
remove_before_ajax_validation:
10063
- UndefinedOffset\NoCaptcha\Forms\NocaptchaField
10164
```
10265
10366
That specific class is already added by default, but you can add others if you find similar situations.
10467
105-
## RequiredFieldsValidator
106-
107-
This is a composable replacement for [`RequiredFields`](https://api.silverstripe.org/4/SilverStripe/Forms/RequiredFields.html). It doesn't perform the internal field validation that validator does, with the assumption that it will be paired with a `SimpleFieldsValidator`. It uses (so has all of the functionality and methods of) the [`ValidatesMultipleFields`](#validatesmultiplefields) trait.
108-
109-
Displays a validation error if the field(s) has no value.
110-
11168
### Known Issues
11269
113-
While this validator can be used to require data in `GridField`s, as of writing this documentation GridFields don't display validation errors. This [was resolved](https://github.com/silverstripe/silverstripe-framework/pull/10015) in Silverstripe 4.10.0, but for anyone using an older version in the meantime [an extension](./02-extensions.md#gridfieldmessagesextension) is included with this module to fix this problem. The `AjaxCompositeValidator` will display validation error messages against GridFields even without that extension.
114-
This applies to the `WarningFieldsValidator` as well.
70+
Some actions (such as delete, archive, and restore) should be allowed even if the data is not valid (we should be allowed to delete an object _especially_ if its data is invalid), but those actions are not validation exempt by default. [Two extensions](./02-extensions.md#dataobjectvalidationexemptionextension-and-gridfielditemrequestvalidationexemptionextension) are provided with this module to remedy this and we strongly recommend applying them.
11571
11672
## WarningFieldsValidator
11773
118-
Similar to `RequiredFieldsValidator` except instead of blocking the item from saving, this allows the item to save and displays a warning rather than a full validation error. It uses (so has all of the functionality and methods of) the [`ValidatesMultipleFields`](#validatesmultiplefields) trait.
74+
Similar to [`RequiredFieldsValidator`](https://api.silverstripe.org/6/SilverStripe/Forms/Validation/RequiredFields.html) except instead of blocking the item from saving, this allows the item to save and displays a warning rather than a full validation error. It uses (so has all of the functionality and methods of) the [`ValidatesMultipleFields`](#validatesmultiplefields) trait.
11975

12076
This can be very useful for alerting users about data that is technically valid but may not provide the results they expect.
12177

@@ -240,29 +196,6 @@ See the Symfony [validation constraints reference](https://symfony.com/doc/curre
240196

241197
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.
242198

243-
## RegexFieldsValidator
244-
245-
> [!WARNING]
246-
> Deprecated! Use `ConstraintsValidator` with a [`Regex` constraint](https://symfony.com/doc/current/reference/constraints/Regex.html) instead.
247-
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.
249-
250-
Any value that cannot be converted to a string cannot be checked against regex and so is ignored, and therefore implicitly passes validation.
251-
252-
In the below example, the `NotOnlyNumbersField` field must match one of the specified regex patterns.
253-
254-
```php
255-
RegexFieldsValidator::create([
256-
'NotOnlyNumbersField' => [
257-
'/(?!^\d+$)^.*?$/' => 'must not consist entirely of numbers',
258-
'/^[\d]$/' => 'must have only one digit',
259-
]
260-
]);
261-
```
262-
263-
**Note:** If any one of the patterns is matched, it passes validation. If none of the patterns match, all of the corresponding messages are displayed, including a generic prefix. So in the above example, if none of the patterns match the value of `NotOnlyNumbersField`, the following validation error message will display:
264-
`The value for "NotOnlyNumbersField" must not consist entirely of numbers or must have only one digit`
265-
266199
## Abstract Validators
267200

268201
### BaseValidator
@@ -283,7 +216,7 @@ It also has an abstract method `getValidationHints()` which has implications for
283216

284217
### FieldHasValueValidator
285218

286-
This abstract class is itself a subclass of `BaseValidator`, and is useful as a superclass for any validator that needs to check if fields have a value. This functionality is used in the [`RequiredFieldsValidator`](#requiredfieldsvalidator), [`WarningFieldsValidator`](#warningfieldsvalidator), and [`DependentRequiredFieldsValidator`](#dependentrequiredfieldsvalidator).
219+
This abstract class is itself a subclass of `BaseValidator`, and is useful as a superclass for any validator that needs to check if fields have a value. This functionality is used in the [`WarningFieldsValidator`](#warningfieldsvalidator) and [`DependentRequiredFieldsValidator`](#dependentrequiredfieldsvalidator).
287220

288221
```php
289222
// Get the actual FormField for the named field.
@@ -331,7 +264,7 @@ class MyFieldValueExtension extends Extension
331264

332265
## ValidatesMultipleFields
333266

334-
This trait is used in both the [`RequiredFieldsValidator`](#requiredfieldsvalidator) and [`WarningFieldsValidator`](#warningfieldsvalidator). It is useful for any validator that can be fed an array of field names that need to be validated.
267+
This trait is used in the [`WarningFieldsValidator`](#warningfieldsvalidator). It is useful for any validator that can be fed an array of field names that need to be validated.
335268

336269
### Usage
337270

0 commit comments

Comments
 (0)