Final Class Yiisoft\Validator\Rule\FilledAtLeastHandler
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Yiisoft\ |
| Uses Traits | Yiisoft\ |
Validates that a minimum number of specified properties are filled.
See also Yiisoft\
Public Methods
| Method | Description | Defined By |
|---|---|---|
| validate() | Yiisoft\ |
Method Details
| public Yiisoft\ | ||
| $value | mixed | |
| $rule | Yiisoft\ |
|
| $context | Yiisoft\ |
|
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;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.