Final Class Yiisoft\Validator\Validator
| Inheritance | Yiisoft\Validator\Validator |
|---|---|
| Implements | Yiisoft\Validator\ValidatorInterface |
The only built-in implementation of {@see ValidatorInterface}, the main class / entry point processing all the data and rules with validation context together and performing the actual validation.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Validator\Validator | |
| validate() | Yiisoft\Validator\Validator | |
| withDefaultSkipOnEmptyCondition() | An immutable setter to change default "skip on empty" condition. | Yiisoft\Validator\Validator |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| DEFAULT_TRANSLATION_CATEGORY | 'yii-validator' | A name for {@see CategorySource} used with translator ({@see TranslatorInterface}) by default. | Yiisoft\Validator\Validator |
Method Details
| public mixed __construct ( Yiisoft\Validator\RuleHandlerResolverInterface|null $ruleHandlerResolver = null, \Yiisoft\Translator\TranslatorInterface|null $translator = null, boolean|callable|null $defaultSkipOnEmpty = null, string $translationCategory = self::DEFAULT_TRANSLATION_CATEGORY, Yiisoft\Validator\PropertyTranslatorInterface|null $defaultPropertyTranslator = null, \Yiisoft\Translator\MessageFormatterInterface|null $messageFormatter = null, string $messageFormatterLocale = 'en-US' ) | ||
| $ruleHandlerResolver | Yiisoft\Validator\RuleHandlerResolverInterface|null |
Optional container to resolve rule handler names to corresponding instances. If not provided, {@see \Yiisoft\Validator\SimpleRuleContainer} used as a default one. |
| $translator | \Yiisoft\Translator\TranslatorInterface|null |
Optional translator instance used for translations of error messages. If not provided, a default one is created via {@see \Yiisoft\Validator\createDefaultTranslator()}. |
| $defaultSkipOnEmpty | boolean|callable|null |
Raw non-normalized "skip on empty" value (see {@see \Yiisoft\Validator\SkipOnEmptyInterface::getSkipOnEmpty()}). |
| $translationCategory | string |
A name for {@see \Yiisoft\Translator\CategorySource} used during creation
({@see \Yiisoft\Validator\createDefaultTranslator()}) of default translator ({@see \Yiisoft\Translator\TranslatorInterface}) in case |
| $defaultPropertyTranslator | Yiisoft\Validator\PropertyTranslatorInterface|null |
A default translator used for translation of rule ({@see \Yiisoft\Validator\RuleInterface}) properties. If not provided, a {@see \Yiisoft\Validator\PropertyTranslator\TranslatorPropertyTranslator} will be used. |
| $messageFormatter | \Yiisoft\Translator\MessageFormatterInterface|null |
A message formatter instance used for formats of error messages that requires format only. If not provided, message is returned as is. |
| $messageFormatterLocale | string |
Locale to use when error message requires format only. |
public function __construct(
?RuleHandlerResolverInterface $ruleHandlerResolver = null,
?TranslatorInterface $translator = null,
bool|callable|null $defaultSkipOnEmpty = null,
string $translationCategory = self::DEFAULT_TRANSLATION_CATEGORY,
?PropertyTranslatorInterface $defaultPropertyTranslator = null,
?MessageFormatterInterface $messageFormatter = null,
string $messageFormatterLocale = 'en-US',
) {
$translator ??= $this->createDefaultTranslator($translationCategory);
$this->ruleHandlerResolver = $ruleHandlerResolver ?? new SimpleRuleHandlerContainer();
$this->defaultSkipOnEmptyCondition = SkipOnEmptyNormalizer::normalize($defaultSkipOnEmpty);
$this->defaultPropertyTranslator = $defaultPropertyTranslator
?? new TranslatorPropertyTranslator($translator);
$this->messageProcessor = new MessageProcessor(
$translator,
$translationCategory,
$messageFormatter ?? new NullMessageFormatter(),
$messageFormatterLocale,
);
}
| public Yiisoft\Validator\Result validate ( mixed $data, callable|iterable|object|string|null $rules = null, Yiisoft\Validator\ValidationContext|null $context = null ) | ||
| $data | mixed | |
| $rules | callable|iterable|object|string|null | |
| $context | Yiisoft\Validator\ValidationContext|null | |
public function validate(
mixed $data,
callable|iterable|object|string|null $rules = null,
?ValidationContext $context = null,
): Result {
$dataSet = DataSetNormalizer::normalize($data);
$originalData = $dataSet instanceof DataWrapperInterface ? $dataSet->getSource() : $data;
$rules = RulesNormalizer::normalize(
$rules,
$dataSet,
$this->defaultSkipOnEmptyCondition,
);
$defaultPropertyTranslator
= ($dataSet instanceof PropertyTranslatorProviderInterface ? $dataSet->getPropertyTranslator() : null)
?? $this->defaultPropertyTranslator;
$context ??= new ValidationContext();
$context
->setContextDataOnce($this, $defaultPropertyTranslator, $data, $dataSet)
->setDataSet($dataSet);
$result = new Result();
foreach ($rules as $property => $propertyRules) {
if (is_int($property)) {
$validatedData = $originalData;
$context->setParameter(ValidationContext::PARAMETER_VALUE_AS_ARRAY, $dataSet->getData());
$context->setProperty(null);
} else {
$validatedData = $dataSet->getPropertyValue($property);
$context->setParameter(ValidationContext::PARAMETER_VALUE_AS_ARRAY, null);
$context->setProperty($property);
}
if ($dataSet instanceof LabelsProviderInterface) {
$labels = $dataSet->getValidationPropertyLabels();
if (is_string($property)) {
$context->setPropertyLabel($labels[$property] ?? $property);
}
} else {
$context->setPropertyLabel(is_string($property) ? $property : $context->getPropertyLabel());
}
$tempResult = $this->validateInternal($validatedData, $propertyRules, $context);
foreach ($tempResult->getErrors() as $error) {
$result->addErrorWithoutPostProcessing(
$this->messageProcessor->process($error),
$error->getParameters(),
$error->getValuePath(),
);
}
}
if ($originalData instanceof PostValidationHookInterface) {
$originalData->processValidationResult($result);
}
return $result;
}
An immutable setter to change default "skip on empty" condition.
| public $this withDefaultSkipOnEmptyCondition ( boolean|callable|null $value ) | ||
| $value | boolean|callable|null |
A new raw non-normalized "skip on empty" value (see {@see \Yiisoft\Validator\SkipOnEmptyInterface::getSkipOnEmpty()}). |
| return | $this |
The new instance with a changed value. |
|---|---|---|
public function withDefaultSkipOnEmptyCondition(bool|callable|null $value): static
{
$new = clone $this;
$new->defaultSkipOnEmptyCondition = SkipOnEmptyNormalizer::normalize($value);
return $new;
}
Signup or Login in order to comment.