silverstripe / silverstripe-framework

Silverstripe Framework, the MVC framework that powers Silverstripe CMS
https://www.silverstripe.org
BSD 3-Clause "New" or "Revised" License
721 stars 821 forks source link

NEW Wire up symfony/validator #11123

Closed GuySartorelli closed 8 months ago

GuySartorelli commented 8 months ago

Description

Wires up symfony/validator so we can use it for robust and potentially complex validation logic.

Usage is:

use SilverStripe\Core\Validation\ConstraintValidator;

/**
 * @var \Symfony\Component\Validator\Constraint $constraint
 * @var \SilverStripe\ORM\ValidationResult $result
 */
$result = ConstraintValidator::validate($valueToValidate, $constraint);

e.g. for testing if a URL is valid:

use SilverStripe\Core\Validation\ConstraintValidator;
use Symfony\Component\Validator\Constraints\Url;

$isValid = ConstraintValidator::validate($url, new Url())->isValid();

Most of the constraints in https://symfony.com/doc/current/reference/constraints.html are explicitly supported, except for the following:

Any which require symfony/expression-language:

Any which require symfony/http-client:

Any which require symfony/intl:

Any which require symfony/doctrine or anything which assumes we're using symfony/validate in the context of setting constraints on metadata objects:

With exception of those last three (which I've mentioned in the docs are explicitly not supported, because it relies on extra wiring up of things that we just aren't gonna do), I'd expect the others to all work - I just haven't included tests for them because we don't have those dependencies.

Issues

Pull request checklist

GuySartorelli commented 8 months ago

Add a unit test(s) for passing in an array of contraints

Done

Add a unit test for passing in optional field param

Done

Also added an exception (and test for it) when passing in an empty array.