Final Class Yiisoft\Validator\Rule\Date\DateTimeHandler
| Inheritance | Yiisoft\Validator\Rule\Date\DateTimeHandler » Yiisoft\Validator\Rule\Date\BaseDateHandler |
|---|---|
| Implements | Yiisoft\Validator\RuleHandlerInterface |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Validator\Rule\Date\DateTimeHandler | |
| validate() | Yiisoft\Validator\Rule\Date\BaseDateHandler |
Method Details
| public mixed __construct ( integer $dateType = IntlDateFormatter::SHORT, integer $timeType = IntlDateFormatter::SHORT, string|null $timeZone = null, string|null $locale = null, string|null $messageFormat = null, integer|null $messageDateType = IntlDateFormatter::SHORT, integer|null $messageTimeType = IntlDateFormatter::SHORT, string $incorrectInputMessage = '{Property} must be a date.', string $tooEarlyMessage = '{Property} must be no early than {limit}.', string $tooLateMessage = '{Property} must be no late than {limit}.' ) | ||
| $dateType | integer | |
| $timeType | integer | |
| $timeZone | string|null | |
| $locale | string|null | |
| $messageFormat | string|null | |
| $messageDateType | integer|null | |
| $messageTimeType | integer|null | |
| $incorrectInputMessage | string | |
| $tooEarlyMessage | string | |
| $tooLateMessage | string | |
public function __construct(
int $dateType = IntlDateFormatter::SHORT,
int $timeType = IntlDateFormatter::SHORT,
?string $timeZone = null,
?string $locale = null,
?string $messageFormat = null,
?int $messageDateType = IntlDateFormatter::SHORT,
?int $messageTimeType = IntlDateFormatter::SHORT,
string $incorrectInputMessage = '{Property} must be a date.',
string $tooEarlyMessage = '{Property} must be no early than {limit}.',
string $tooLateMessage = '{Property} must be no late than {limit}.',
) {
parent::__construct(
$dateType,
$timeType,
$timeZone,
$locale,
$messageFormat,
$messageDateType,
$messageTimeType,
$incorrectInputMessage,
$tooEarlyMessage,
$tooLateMessage,
);
}
| 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 Date && !$rule instanceof DateTime && !$rule instanceof Time) {
throw new UnexpectedRuleException([Date::class, DateTime::class, Time::class], $rule);
}
$timeZone = $rule->getTimeZone() ?? $this->timeZone;
if ($timeZone !== null) {
$timeZone = new DateTimeZone($timeZone);
}
$result = new Result();
$date = $this->prepareValue($value, $rule, $timeZone, false);
if ($date === null) {
$result->addError(
$rule->getIncorrectInputMessage() ?? $this->incorrectInputMessage,
[
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
],
);
return $result;
}
$min = $this->prepareValue($rule->getMin(), $rule, $timeZone, true);
if ($min !== null && $date < $min) {
$result->addError(
$rule->getTooEarlyMessage() ?? $this->tooEarlyMessage,
[
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
'value' => $this->formatDate($date, $rule, $timeZone),
'limit' => $this->formatDate($min, $rule, $timeZone),
],
);
return $result;
}
$max = $this->prepareValue($rule->getMax(), $rule, $timeZone, true);
if ($max !== null && $date > $max) {
$result->addError(
$rule->getTooLateMessage() ?? $this->tooLateMessage,
[
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
'value' => $this->formatDate($date, $rule, $timeZone),
'limit' => $this->formatDate($max, $rule, $timeZone),
],
);
return $result;
}
return $result;
}
Signup or Login in order to comment.