Neos\Flow\Validation\Validator\DisjunctionValidator::validate PHP Method

validate() public method

So only one validator has to be valid, to make the whole disjunction valid. Errors are only returned if all validators failed.
public validate ( mixed $value ) : Neos\Error\Messages\Result
$value mixed The value that should be validated
return Neos\Error\Messages\Result
    public function validate($value)
    {
        $validators = $this->getValidators();
        if ($validators->count() > 0) {
            $result = null;
            foreach ($validators as $validator) {
                $validatorResult = $validator->validate($value);
                if ($validatorResult->hasErrors()) {
                    if ($result === null) {
                        $result = $validatorResult;
                    } else {
                        $result->merge($validatorResult);
                    }
                } else {
                    if ($result === null) {
                        $result = $validatorResult;
                    } else {
                        $result->clear();
                    }
                    break;
                }
            }
        } else {
            $result = new ErrorResult();
        }
        return $result;
    }

Usage Example

 /**
  * @test
  */
 public function validateReturnsAllErrorsIfAllValidatorsReturnErrrors()
 {
     $validatorDisjunction = new DisjunctionValidator([]);
     $error1 = new Error\Error('Error', 123);
     $error2 = new Error\Error('Error2', 123);
     $errors1 = new Error\Result();
     $errors1->addError($error1);
     $validatorObject = $this->createMock(ValidatorInterface::class);
     $validatorObject->expects($this->any())->method('validate')->will($this->returnValue($errors1));
     $errors2 = new Error\Result();
     $errors2->addError($error2);
     $secondValidatorObject = $this->createMock(ValidatorInterface::class);
     $secondValidatorObject->expects($this->any())->method('validate')->will($this->returnValue($errors2));
     $validatorDisjunction->addValidator($validatorObject);
     $validatorDisjunction->addValidator($secondValidatorObject);
     $this->assertEquals([$error1, $error2], $validatorDisjunction->validate('some subject')->getErrors());
 }
DisjunctionValidator