Final Class Yiisoft\Validator\Rule\Date\DateHandler
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Yiisoft\ |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| validate() | Yiisoft\ |
Method Details
| public mixed __construct ( integer $dateType = IntlDateFormatter::SHORT, ?string $timeZone = null, ?string $locale = null, ?string $messageFormat = null, ?int $messageDateType = IntlDateFormatter::SHORT, string $incorrectInputMessage = '{Property} must be a date.', string $tooEarlyMessage = '{Property} must be no earlier than {limit}.', string $tooLateMessage = '{Property} must be no later than {limit}.' ) | ||
| $dateType | integer | |
| $timeZone | ?string | |
| $locale | ?string | |
| $messageFormat | ?string | |
| $messageDateType | ?int | |
| $incorrectInputMessage | string | |
| $tooEarlyMessage | string | |
| $tooLateMessage | string | |
public function __construct(
int $dateType = IntlDateFormatter::SHORT,
?string $timeZone = null,
?string $locale = null,
?string $messageFormat = null,
?int $messageDateType = IntlDateFormatter::SHORT,
string $incorrectInputMessage = '{Property} must be a date.',
string $tooEarlyMessage = '{Property} must be no earlier than {limit}.',
string $tooLateMessage = '{Property} must be no later than {limit}.',
) {
parent::__construct(
$dateType,
IntlDateFormatter::NONE,
$timeZone,
$locale,
$messageFormat,
$messageDateType,
IntlDateFormatter::NONE,
$incorrectInputMessage,
$tooEarlyMessage,
$tooLateMessage,
);
}
| public Yiisoft\ | ||
| $value | mixed | |
| $rule | Yiisoft\ |
|
| $context | Yiisoft\ |
|
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;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.