Final Class Yiisoft\Validator\Rule\FilledAtLeastHandler
| Inheritance | Yiisoft\Validator\Rule\FilledAtLeastHandler |
|---|---|
| Implements | Yiisoft\Validator\RuleHandlerInterface |
| Uses Traits | Yiisoft\Validator\Rule\Trait\TranslatedPropertiesHandlerTrait |
Validates that a minimum number of specified properties are filled.
See also Yiisoft\Validator\Rule\FilledAtLeast.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| validate() | Yiisoft\Validator\Rule\FilledAtLeastHandler |
Method Details
| public Yiisoft\Validator\Result validate ( mixed $value, Yiisoft\Validator\RuleInterface $rule, Yiisoft\Validator\ValidationContext $context ) | ||
| $value | mixed | |
| $rule | Yiisoft\Validator\RuleInterface | |
| $context | Yiisoft\Validator\ValidationContext | |
public function validate(mixed $value, RuleInterface $rule, ValidationContext $context): Result
{
if (!$rule instanceof FilledAtLeast) {
throw new UnexpectedRuleException(FilledAtLeast::class, $rule);
}
/** @var mixed $value */
$value = $context->getParameter(ValidationContext::PARAMETER_VALUE_AS_ARRAY) ?? $value;
$result = new Result();
if (!is_array($value) && !is_object($value)) {
return $result->addError($rule->getIncorrectInputMessage(), [
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
'type' => get_debug_type($value),
]);
}
$filledCount = 0;
foreach ($rule->getProperties() as $property) {
if (!(new WhenEmpty())(ArrayHelper::getValue($value, $property), $context->isPropertyMissing())) {
$filledCount++;
}
}
if ($filledCount < $rule->getMin()) {
$result->addError($rule->getMessage(), [
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
'properties' => $this->getFormattedPropertiesString($rule->getProperties(), $context),
'Properties' => $this->getCapitalizedPropertiesString($rule->getProperties(), $context),
'min' => $rule->getMin(),
]);
}
return $result;
}
Signup or Login in order to comment.