6

I have a field in a request which can either be email or phone. Now I need to validate it against two constraints - the standard email constraint and a custom phone. I know that I can pass an array of constrains link this:

$constraint = new Constraints\Collection([
            'name' => new Constraints\NotBlank(),
            'uid' => [new Constraints\Email(), new Constraints\Phone()],
            'pass' => new Constraints\NotBlank()
        ]);

But Validator applies the AND logic to the array of constraints, I need OR so that if the value fits one of them the validation succeeds. Is that possible by any standard Symfony means?

super.t
  • 2,526
  • 7
  • 32
  • 51

2 Answers2

5

There is a special constraint for that: https://symfony.com/doc/current/reference/constraints/AtLeastOneOf.html

Ilya Levin
  • 351
  • 3
  • 9
4

Create a custom validator and implement your own logic with existing constraint. AFAIK that's no other way to obtain what you're asking for.

DonCallisto
  • 29,419
  • 9
  • 72
  • 100