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

NEW Wire up symfony/validator #11123

Merged

Conversation

GuySartorelli
Copy link
Member

@GuySartorelli GuySartorelli commented Jan 31, 2024

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:

  • Symfony\Component\Validator\Constraints\Expression
  • Symfony\Component\Validator\Constraints\ExpressionSyntax
  • Symfony\Component\Validator\Constraints\When

Any which require symfony/http-client:

  • Symfony\Component\Validator\Constraints\NotCompromisedPassword

Any which require symfony/intl:

  • Symfony\Component\Validator\Constraints\Bic
  • Symfony\Component\Validator\Constraints\Country
  • Symfony\Component\Validator\Constraints\Currency
  • Symfony\Component\Validator\Constraints\Language
  • Symfony\Component\Validator\Constraints\Locale

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

  • Symfony\Component\Validator\Constraints\Cascade
  • Symfony\Component\Validator\Constraints\Traverse
  • Symfony\Component\Validator\Constraints\Valid

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

  • The target branch is correct
  • All commits are relevant to the purpose of the PR (e.g. no debug statements, unrelated refactoring, or arbitrary linting)
    • Small amounts of additional linting are usually okay, but if it makes it hard to concentrate on the relevant changes, ask for the unrelated changes to be reverted, and submitted as a separate PR.
  • The commit messages follow our commit message guidelines
  • The PR follows our contribution guidelines
  • Code changes follow our coding conventions
  • This change is covered with tests (or tests aren't necessary for this change)
  • Any relevant User Help/Developer documentation is updated; for impactful changes, information is added to the changelog for the intended release
  • CI is green

Copy link
Member

@emteknetnz emteknetnz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Add a unit test for passing in optional field param

src/Core/Validation/ConstraintValidator.php Outdated Show resolved Hide resolved
@GuySartorelli
Copy link
Member Author

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.

@emteknetnz emteknetnz merged commit 7f71695 into silverstripe:5 Feb 2, 2024
15 checks passed
@emteknetnz emteknetnz deleted the pulls/5/symfony-validator branch February 2, 2024 00:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants