0 follower

Final Class Yiisoft\Validator\Rule\FilledAtLeastHandler

InheritanceYiisoft\Validator\Rule\FilledAtLeastHandler
ImplementsYiisoft\Validator\RuleHandlerInterface
Uses TraitsYiisoft\Validator\Rule\Trait\TranslatedPropertiesHandlerTrait

Validates that a minimum number of specified properties are filled.

See also Yiisoft\Validator\Rule\FilledAtLeast.

Method Details

Hide inherited methods

validate() public method

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;
}