Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC Document using symfony/validator #454

Merged
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
29 changes: 29 additions & 0 deletions en/02_Developer_Guides/00_Model/09_Validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions en/02_Developer_Guides/03_Forms/01_Validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
28 changes: 28 additions & 0 deletions en/04_Changelogs/5.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
Loading