Final Class Yiisoft\Validator\Rule\Date\Date
Defines validation options to check that the value is a date.
A simple example of how to use it to validate a date:
$rules = [
'date' => new Yiisoft\Validator\Rule\Date\Date(format: 'php:Y-m-d'),
];
In the example above, the PHP attributes equivalent will be:
use Yiisoft\Validator\Validator;
use Yiisoft\Validator\Rule\Date\Date;
final class User
{
public function __construct(
#[Date(format: 'php:Y-m-d')]
public string $date,
) }
}
$user = new User(date: '2022-01-01');
$validator = (new Validator())->validate($user);
See also Yiisoft\Validator\Rule\Date\DateHandler.
Public Methods
Method Details
| public mixed __construct ( string|null $format = null, integer|null $dateType = null, string|null $timeZone = null, string|null $locale = null, DateTimeInterface|integer|string|null $min = null, DateTimeInterface|integer|string|null $max = null, string|null $messageFormat = null, integer|null $messageDateType = null, string|null $incorrectInputMessage = null, string|null $tooEarlyMessage = null, string|null $tooLateMessage = null, mixed|null $skipOnEmpty = null, boolean $skipOnError = false, Closure|null $when = null ) | ||
| $format | string|null |
The date format that the value being validated should follow. This can be a date pattern as described in the ICU manual. Alternatively this can be a string prefixed with Here are some example values:
|
| $dateType | integer|null |
Format of the date determined by one of the |
| $timeZone | string|null |
The timezone to use for parsing and formatting date values. This can be any value
that may be passed to
date_default_timezone_set()> e.g. |
| $locale | string|null |
Locale to use when formatting or parsing or |
| $min | DateTimeInterface|integer|string|null |
Lower limit of the date. Defaults to |
| $max | DateTimeInterface|integer|string|null |
Upper limit of the date. Defaults to |
| $messageFormat | string|null |
Date format that is used in error messages. |
| $messageDateType | integer|null |
One of the |
| $incorrectInputMessage | string|null |
A message used when the validated value is not valid date. You may use the following placeholders in the message:
|
| $tooEarlyMessage | string|null |
A message used when the validated date is less than {@see $min}. You may use the following placeholders in the message:
|
| $tooLateMessage | string|null |
A message used when the validated date is more than {@see $max}. You may use the following placeholders in the message:
|
| $skipOnEmpty | mixed|null |
Whether to skip this rule if the value validated is empty. See {@see \Yiisoft\Validator\SkipOnEmptyInterface}. |
| $skipOnError | boolean |
Whether to skip this rule if any of the previous rules gave an error. See {@see \Yiisoft\Validator\Rule\Date\SkipOnErrorInterface}. |
| $when | Closure|null |
A callable to define a condition for applying the rule. See {@see \Yiisoft\Validator\WhenInterface}. |
public function __construct(
?string $format = null,
private readonly ?int $dateType = null,
?string $timeZone = null,
?string $locale = null,
int|string|DateTimeInterface|null $min = null,
int|string|DateTimeInterface|null $max = null,
?string $messageFormat = null,
private readonly ?int $messageDateType = null,
?string $incorrectInputMessage = null,
?string $tooEarlyMessage = null,
?string $tooLateMessage = null,
bool|callable|null $skipOnEmpty = null,
bool $skipOnError = false,
?Closure $when = null,
) {
parent::__construct(
$format,
$timeZone,
$locale,
$min,
$max,
$messageFormat,
$incorrectInputMessage,
$tooEarlyMessage,
$tooLateMessage,
$skipOnEmpty,
$skipOnError,
$when,
);
}
| public string|null getIncorrectInputMessage ( ) |
public function getIncorrectInputMessage(): ?string
{
return $this->incorrectInputMessage;
}
Defined in: Yiisoft\Validator\Rule\Date\BaseDate::getMax()
| public DateTimeInterface|integer|string|null getMax ( ) |
public function getMax(): DateTimeInterface|int|string|null
{
return $this->max;
}
| public integer|null getMessageDateType ( ) |
public function getMessageDateType(): ?int
{
return $this->messageDateType;
}
| public string|null getMessageFormat ( ) |
public function getMessageFormat(): ?string
{
return $this->messageFormat;
}
Defined in: Yiisoft\Validator\Rule\Date\BaseDate::getMin()
| public DateTimeInterface|integer|string|null getMin ( ) |
public function getMin(): DateTimeInterface|int|string|null
{
return $this->min;
}
Defined in: Yiisoft\Validator\Rule\Date\BaseDate::getName()
| public string getName ( ) |
public function getName(): string
{
return 'date';
}
Defined in: Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait::getSkipOnEmpty()
A getter for $skipOnEmpty property.
| public boolean|callable|null getSkipOnEmpty ( ) | ||
| return | boolean|callable|null |
A current raw (non-normalized) value. |
|---|---|---|
public function getSkipOnEmpty(): bool|callable|null
{
return $this->skipOnEmpty;
}
| public non-empty-string|null getTimeZone ( ) |
public function getTimeZone(): ?string
{
return $this->timeZone;
}
| public string|null getTooEarlyMessage ( ) |
public function getTooEarlyMessage(): ?string
{
return $this->tooEarlyMessage;
}
| public string|null getTooLateMessage ( ) |
public function getTooLateMessage(): ?string
{
return $this->tooLateMessage;
}
Defined in: Yiisoft\Validator\Rule\Trait\WhenTrait::getWhen()
A getter for $when property.
| public Closure|null getWhen ( ) | ||
| return | Closure|null |
Current value:
|
|---|---|---|
public function getWhen(): ?Closure
{
return $this->when;
}
Defined in: Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait::shouldSkipOnError()
A getter for $skipOnError property.
| public boolean shouldSkipOnError ( ) | ||
| return | boolean |
Current value. |
|---|---|---|
public function shouldSkipOnError(): bool
{
return $this->skipOnError;
}
Defined in: Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait::skipOnEmpty()
An immutable setter to change $skipOnEmpty property.
| public $this skipOnEmpty ( boolean|callable|null $value ) | ||
| $value | boolean|callable|null |
A new value. |
| return | $this |
The new instance with a changed value. |
|---|---|---|
public function skipOnEmpty(bool|callable|null $value): static
{
$new = clone $this;
$new->skipOnEmpty = $value;
return $new;
}
Defined in: Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait::skipOnError()
An immutable setter to change $skipOnError property.
| public $this skipOnError ( boolean $value ) | ||
| $value | boolean |
A new value. |
| return | $this |
The new instance with a changed value. |
|---|---|---|
public function skipOnError(bool $value): static
{
$new = clone $this;
$new->skipOnError = $value;
return $new;
}
Defined in: Yiisoft\Validator\Rule\Trait\WhenTrait::when()
An immutable setter to change $when property.
| public $this when ( Closure|null $value ) | ||
| $value | Closure|null |
A new value:
|
| return | $this |
The new instance with a changed value. |
|---|---|---|
public function when(?Closure $value): static
{
$new = clone $this;
$new->when = $value;
return $new;
}
Signup or Login in order to comment.