Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate against several constraints with OR logic in Symfony

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?

like image 879
super.t Avatar asked May 25 '18 07:05

super.t


1 Answers

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.

like image 183
DonCallisto Avatar answered Sep 18 '22 16:09

DonCallisto