Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@ jobs:
permissions:
pull-requests: read
contents: read
actions: write
uses: silverstripe/gha-ci/.github/workflows/ci.yml@v1
permissions:
pull-requests: read
contents: write
with:
extra_jobs: |
- php: 8.1
- php: 8.3
phpunit: false
endtoend: true
endtoend_suite: admin
endtoend_config: vendor/silverstripe/admin/behat.yml
- php: 8.2
- php: 8.4
phpunit: false
endtoend: true
endtoend_suite: cms
Expand Down
14 changes: 2 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@ composer require guysartorelli/silverstripe-composable-validators

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.

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).
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).

## [Available Validators][4]

- **[`AjaxCompositeValidator`][5]**
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.
- **[`SimpleFieldsValidator`][8]**
Ensures the internal validation of form fields by calling `validate()` on them.
- **[`RequiredFieldsValidator`][9]**
Like Silverstripe's [`RequiredFields`][10] validator, but more convenient for use in a `CompositeValidator`.
- **[`WarningFieldsValidator`][11]**
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
- **[`DependentRequiredFieldsValidator`][12]**
Expand All @@ -40,8 +36,6 @@ Uses [`SearchFilter`s][13] to define fields as required conditionally, based on
Require a specific [elemental block(s)][15] to exist in the `ElementalArea`, with optional minimum and maximum numbers of blocks and optional positional validation.
- **[`ConstraintsValidator`][16]**
Validate values against [`symfony/validation` constraints](https://symfony.com/doc/current/reference/constraints.html). This is super powerful - definitely check it out.
- **[`RegexFieldsValidator`][17]** (deprecated)
Ensure some field(s) matches a specified regex pattern.

### [Abstract Validators][18]

Expand All @@ -63,18 +57,14 @@ Like `ValidatesMultipleFields` but requires a configuration array for each field
[3]: https://github.com/UndefinedOffset/silverstripe-nocaptcha
[4]: docs/en/01-validators.md
[5]: docs/en/01-validators.md#ajaxcompositevalidator
[6]: https://api.silverstripe.org/4/SilverStripe/Forms/CompositeValidator.html
[6]: https://api.silverstripe.org/6/SilverStripe/Forms/Validation/CompositeValidator.html
[7]: https://github.com/silverstripe/silverstripe-elemental/issues/764
[8]: docs/en/01-validators.md#simplefieldsvalidator
[9]: docs/en/01-validators.md#requiredfieldsvalidator
[10]: https://api.silverstripe.org/4/SilverStripe/Forms/RequiredFields.html
[11]: docs/en/01-validators.md#warningfieldsvalidator
[12]: docs/en/01-validators.md#dependentrequiredfieldsvalidator
[13]: https://docs.silverstripe.org/en/developer_guides/model/searchfilters/
[14]: docs/en/01-validators.md#requiredblocksvalidator
[15]: https://github.com/silverstripe/silverstripe-elemental
[16]: docs/en/01-validators.md#constraintsvalidator
[17]: docs/en/01-validators.md#regexfieldsvalidator
[18]: docs/en/01-validators.md#abstract-validators
[19]: docs/en/01-validators.md#basevalidator
[20]: docs/en/01-validators.md#fieldhasvaluevalidator
Expand Down
10 changes: 0 additions & 10 deletions _config/config.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
---
Name: guysartorelli-composable-validators-config
---
# Don't validate the NocaptchaField during AJAX validation as it can only be validated once.
Signify\ComposableValidators\Validators\SimpleFieldsValidator:
ignore_field_classes_on_ajax:
- UndefinedOffset\NoCaptcha\Forms\NocaptchaField

# Add styling to CMS
SilverStripe\Admin\LeftAndMain:
extra_requirements_css:
- 'guysartorelli/silverstripe-composable-validators:client/dist/left-and-main.css'

# Replace new FieldsValidator with our SimpleFieldsValidator
SilverStripe\Core\Injector\Injector:
SilverStripe\Forms\FieldsValidator:
class: Signify\ComposableValidators\Validators\SimpleFieldsValidator
4 changes: 2 additions & 2 deletions _config/extensions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ SilverStripe\Forms\Form:
extensions:
- Signify\ComposableValidators\Extensions\FormExtension

SilverStripe\Forms\FormField:
SilverStripe\Admin\LeftAndMain:
extensions:
- Signify\ComposableValidators\Extensions\FormFieldExtension
- Signify\ComposableValidators\Extensions\LeftAndMainAjaxValidationExtension
16 changes: 7 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"keywords": [
"silverstripe",
"composable",
"composite",
"CompositeValidator",
"validator",
"validators",
Expand All @@ -19,7 +18,6 @@
"frontend",
"dependent",
"required",
"regex",
"warning",
"elemental"
],
Expand All @@ -37,15 +35,15 @@
"issues": "https://github.com/GuySartorelli/silverstripe-composable-validators/issues"
},
"require": {
"php": "^8.1",
"silverstripe/framework": "^5.2"
"php": "^8.3",
"silverstripe/framework": "^6"
},
"require-dev": {
"silverstripe/cms": "^5",
"silverstripe/asset-admin": "^2",
"dnadesign/silverstripe-elemental": "^5",
"silverstripe/frameworktest": "^1",
"silverstripe/recipe-testing": "^3"
"silverstripe/cms": "^6",
"silverstripe/asset-admin": "^3",
"dnadesign/silverstripe-elemental": "^6",
"silverstripe/frameworktest": "^2",
"silverstripe/recipe-testing": "^4"
},
"extra": {
"expose": [
Expand Down
103 changes: 18 additions & 85 deletions docs/en/01-validators.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,28 @@ All of these validators can be used in the CMS _and_ in the front-end.

## AjaxCompositeValidator

**Important:** See the [extensions docs](./02-extensions.md) for extensions that are highly recommended if you intend to use this validator.
> [!IMPORTANT]
> See the [extensions docs](./02-extensions.md) for extensions that are highly recommended if you intend to use this validator.

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.

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

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.
This is typically faster as it doesn't need to reload and re-render the form after validating to display the error messages.

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.

### Usage

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.
> [!HINT]
> See the [optional configuration](./02-extensions.md) for information about replacing the default `CompositeValidator` with `AjaxCompositeValidator`.

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()`).
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()`).

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

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

### Known Issues

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.
### Exclude fields from AJAX validation

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

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).

### Usage
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.

In most situations this validator will require no configuration at all.

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.

```PHP
// In the DataObject which needs the custom validation
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->add(SomeField::create('FieldName')->setOmitFieldValidation(true));
return $fields;
}
public function getCMSCompositeValidator() : CompositeValidator
{
$validator = parent::getCMSCompositeValidator();
$validator->addValidator(CustomValidator::create());
return $validator;
}

// In your CustomValidator
public function php($data)
{
$valid = true;
/* Other custom validation here */
if ($fieldNeedsValidation) {
$someField = $this->form->Fields()->dataFieldByName('FieldName');
$valid = $someField->validate($this) && $valid;
}
return $valid;
}
```

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.

You can do this by setting the class name for that `FormField` in the `SimpleFieldsValidator`'s `ignore_field_classes_on_ajax` config array.
You can do this by setting the class name for that `FormField` in the `AjaxCompositeValidator`'s `remove_before_ajax_validation` config array.

```yml
Signify\ComposableValidators\Validators\SimpleFieldsValidator:
ignore_field_classes_on_ajax:
Signify\ComposableValidators\Validators\AjaxCompositeValidator:
remove_before_ajax_validation:
- UndefinedOffset\NoCaptcha\Forms\NocaptchaField
```

That specific class is already added by default, but you can add others if you find similar situations.

## RequiredFieldsValidator

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.

Displays a validation error if the field(s) has no value.

### Known Issues

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.
This applies to the `WarningFieldsValidator` as well.
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.

## WarningFieldsValidator

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

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

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

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.

## RegexFieldsValidator

> [!WARNING]
> Deprecated! Use `ConstraintsValidator` with a [`Regex` constraint](https://symfony.com/doc/current/reference/constraints/Regex.html) instead.

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.

Any value that cannot be converted to a string cannot be checked against regex and so is ignored, and therefore implicitly passes validation.

In the below example, the `NotOnlyNumbersField` field must match one of the specified regex patterns.

```php
RegexFieldsValidator::create([
'NotOnlyNumbersField' => [
'/(?!^\d+$)^.*?$/' => 'must not consist entirely of numbers',
'/^[\d]$/' => 'must have only one digit',
]
]);
```

**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:
`The value for "NotOnlyNumbersField" must not consist entirely of numbers or must have only one digit`

## Abstract Validators

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

### FieldHasValueValidator

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).
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).

```php
// Get the actual FormField for the named field.
Expand Down Expand Up @@ -331,7 +264,7 @@ class MyFieldValueExtension extends Extension

## ValidatesMultipleFields

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

### Usage

Expand Down
Loading