public createValidator ( string $validatorType, array $validatorOptions = [] ) : Neos\Flow\Validation\Validator\ValidatorInterface | ||
$validatorType | string | Either one of the built-in data types or fully qualified validator class name |
$validatorOptions | array | Options to be passed to the validator |
return | Neos\Flow\Validation\Validator\ValidatorInterface |
public function createValidator($validatorType, array $validatorOptions = [])
{
$validatorObjectName = $this->resolveValidatorObjectName($validatorType);
if ($validatorObjectName === false) {
return null;
}
switch ($this->objectManager->getScope($validatorObjectName)) {
case Configuration::SCOPE_PROTOTYPE:
$validator = new $validatorObjectName($validatorOptions);
break;
case Configuration::SCOPE_SINGLETON:
if (count($validatorOptions) > 0) {
throw new Exception\InvalidValidationConfigurationException('The validator "' . $validatorObjectName . '" is of scope singleton, but configured to be used with options. A validator with options must be of scope prototype.', 1358958575);
}
$validator = $this->objectManager->get($validatorObjectName);
break;
default:
throw new Exception\NoSuchValidatorException('The validator "' . $validatorObjectName . '" is not of scope singleton or prototype!', 1300694835);
}
if (!$validator instanceof ValidatorInterface) {
throw new Exception\NoSuchValidatorException(sprintf('The validator "%s" does not implement %s!', $validatorObjectName, ValidatorInterface::class), 1300694875);
}
return $validator;
}
/** * Checks for a collection and if needed validates the items in the collection. * This is done with the specified element validator or a validator based on * the given element type and validation group. * * Either elementValidator or elementType must be given, otherwise validation * will be skipped. * * @param mixed $value A collection to be validated * @return void */ protected function isValid($value) { foreach ($value as $index => $collectionElement) { if (isset($this->options['elementValidator'])) { $collectionElementValidator = $this->validatorResolver->createValidator($this->options['elementValidator'], $this->options['elementValidatorOptions']); } elseif (isset($this->options['elementType'])) { if (isset($this->options['validationGroups'])) { $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType'], $this->options['validationGroups']); } else { $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType']); } } else { return; } if ($collectionElementValidator instanceof ObjectValidatorInterface) { $collectionElementValidator->setValidatedInstancesContainer($this->validatedInstancesContainer); } $this->result->forProperty($index)->merge($collectionElementValidator->validate($collectionElement)); } }