Final Class Yiisoft\Validator\Rule\RequiredHandler
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Yiisoft\ |
A handler for {@see Required} rule. Validates that the specified value is passed and is not empty.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| validate() | Yiisoft\ |
Method Details
| public mixed __construct ( callable|null $defaultEmptyCondition = null ) | ||
| $defaultEmptyCondition | callable|null |
A default empty condition used to determine emptiness of the value. |
public function __construct(
?callable $defaultEmptyCondition = null,
) {
$this->defaultEmptyCondition = $defaultEmptyCondition ?? new WhenEmpty(trimString: true);
}
| public Yiisoft\ | ||
| $value | mixed | |
| $rule | Yiisoft\ |
|
| $context | Yiisoft\ |
|
public function validate(mixed $value, RuleInterface $rule, ValidationContext $context): Result
{
if (!$rule instanceof Required) {
throw new UnexpectedRuleException(Required::class, $rule);
}
$result = new Result();
if ($context->isPropertyMissing()) {
$result->addError($rule->getNotPassedMessage(), [
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
]);
return $result;
}
$emptyCondition = $rule->getEmptyCondition() ?? $this->defaultEmptyCondition;
if (!$emptyCondition($value, $context->isPropertyMissing())) {
return $result;
}
$result->addError($rule->getMessage(), [
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
]);
return $result;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.