From ec022002d7eb6f676095b0e8fa0236ced5608be1 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Thu, 1 Feb 2024 12:49:46 +1300 Subject: [PATCH] DOC Document using symfony/validator --- .../00_Model/09_Validation.md | 29 +++++++++++++++++++ .../03_Forms/01_Validation.md | 4 +++ en/04_Changelogs/5.2.0.md | 28 ++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/en/02_Developer_Guides/00_Model/09_Validation.md b/en/02_Developer_Guides/00_Model/09_Validation.md index 1f16aeda7..fd833ee3c 100644 --- a/en/02_Developer_Guides/00_Model/09_Validation.md +++ b/en/02_Developer_Guides/00_Model/09_Validation.md @@ -6,6 +6,35 @@ icon: check-square # Validation and constraints +## Validation using `symfony/validator` constraints {#symfony-validator} + +The [`ConstraintValidator`](api:SilverStripe\Core\Validation\ConstraintValidator) class provides an abstraction around [`symfony/validator`](https://symfony.com/doc/current/components/validator.html), so you can easily validate values against symfony's validation constraints and get a [`ValidationResult`](api:SilverStripe\ORM\ValidationResult) object as a result. + +```php +use SilverStripe\Core\Validation\ConstraintValidator; + +/** + * @var \Symfony\Component\Validator\Constraint $constraint + * @var \SilverStripe\ORM\ValidationResult $result + */ +$result = ConstraintValidator::validate($valueToValidate, $constraint); +``` + +To test if a URL is valid, for example: + +```php +use SilverStripe\Core\Validation\ConstraintValidator; +use Symfony\Component\Validator\Constraints\Url; + +$isValid = ConstraintValidator::validate($url, new Url())->isValid(); +``` + +You can use most of the constraints listed in Symfony's [supported constraints](https://symfony.com/doc/current/reference/constraints.html) documentation, though note that some of them require additional symfony dependencies. + +Validation using constraints that rely on `symfony/doctrine` is explicitly not supported in Silverstripe CMS. + +## Model validation + Traditionally, validation in Silverstripe CMS has been mostly handled through [form validation](../forms). While this is a useful approach, it can lead to data inconsistencies if the record is modified outside of the form context. diff --git a/en/02_Developer_Guides/03_Forms/01_Validation.md b/en/02_Developer_Guides/03_Forms/01_Validation.md index 531de11fc..90d98f1e6 100644 --- a/en/02_Developer_Guides/03_Forms/01_Validation.md +++ b/en/02_Developer_Guides/03_Forms/01_Validation.md @@ -6,6 +6,10 @@ icon: check-square # Form validation +> [!HINT] +> Before you start implementing custom validation logic, check out [validation using `symfony/validator` constraints](/developer_guides/model/validation/#validation-and-constraints) +> and see if there's an existing constraint that can do the heavy lifting for you. + Silverstripe CMS provides server-side form validation out of the box through the [Validator](api:SilverStripe\Forms\Validator) abstract class and its' child classes (see [available validators](#available-validators) below). A single `Validator` instance is set on each `Form`. Validators are implemented as an argument to the [Form](api:SilverStripe\Forms\Form) constructor or through the function `setValidator`. diff --git a/en/04_Changelogs/5.2.0.md b/en/04_Changelogs/5.2.0.md index 46ba83e42..de36ced2a 100644 --- a/en/04_Changelogs/5.2.0.md +++ b/en/04_Changelogs/5.2.0.md @@ -13,6 +13,7 @@ title: 5.2.0 (unreleased) - [ErrorPage allowed codes configuration](#errorpage-allowed-codes-configuration) - [Create random passwords for new users](#create-random-passwords-for-new-users) - [Buttons to select all files and deselect all files](#bulk-action-buttons) + - [Support for validation using `symfony/validator`](#symfony-validator) - [New searchable dropdown fields](#searchable-dropdown-field) - [Create file variants with different extensions](#file-variants) - [More nuanced permissions for `/dev/*` routes](#dev-route-permissions) @@ -172,6 +173,33 @@ The files section of the CMS now has buttons to select and deselect all files an ![select all functionality in the CMS](../_images/asset-admin-select-all.png) +### Support for validation using `symfony/validator`{#symfony-validator} + +A new [`ConstraintValidator`](api:SilverStripe\Core\Validation\ConstraintValidator) class has been added which provides an abstraction around [`symfony/validator`](https://symfony.com/doc/current/components/validator.html), so you can easily validate values against symfony's validation constraints and get a [`ValidationResult`](api:SilverStripe\ORM\ValidationResult) object as a result. + +```php +use SilverStripe\Core\Validation\ConstraintValidator; + +/** + * @var \Symfony\Component\Validator\Constraint $constraint + * @var \SilverStripe\ORM\ValidationResult $result + */ +$result = ConstraintValidator::validate($valueToValidate, $constraint); +``` + +To test if a URL is valid, for example: + +```php +use SilverStripe\Core\Validation\ConstraintValidator; +use Symfony\Component\Validator\Constraints\Url; + +$isValid = ConstraintValidator::validate($url, new Url())->isValid(); +``` + +You can use most of the constraints listed in Symfony's [supported constraints](https://symfony.com/doc/current/reference/constraints.html) documentation, though note that some of them require additional symfony dependencies. + +We explicitly don't support validation using constraints that rely on `symfony/doctrine`. + ### New searchable dropdown fields {#searchable-dropdown-field} A pair of new dropdown form fields have been added which are particularly useful for dropdowns with a large number of options.