Final Class Yiisoft\Validator\Validator
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Yiisoft\ |
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\ |
|
| validate() | Yiisoft\ |
|
| withDefaultSkipOnEmptyCondition() | An immutable setter to change default "skip on empty" condition. | Yiisoft\ |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| DEFAULT_TRANSLATION_CATEGORY | 'yii-validator' | A name for {@see CategorySource} used with translator ({@see TranslatorInterface}) by default. | Yiisoft\ |
Method Details
| public mixed __construct ( Yiisoft\ | ||
| $ruleHandlerResolver | Yiisoft\ |
Optional container to resolve rule handler names to
corresponding instances. If not provided, {@see \ |
| $translator | \ |
Optional translator instance used for translations of error messages.
If not provided, a default one is created via {@see \ |
| $defaultSkipOnEmpty | boolean|callable|null |
Raw non-normalized "skip on empty" value (see
{@see \ |
| $translationCategory | string |
A name for {@see \ |
| $defaultPropertyTranslator | Yiisoft\ |
A default translator used for translation of
rule ({@see \ |
| $messageFormatter | \ |
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\ | ||
| $data | mixed | |
| $rules | callable|iterable|object|string|null | |
| $context | ?\ |
|
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 \ |
| 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;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.