Final Class Yiisoft\Validator\Rule\Date\DateTime
Defines validation options to check that the value is a date with time.
A simple example of how to use it to validate a date with time:
$rules = [
'date' => new Yiisoft\Validator\Rule\Date\DateTime(format: 'php:Y-m-d, H:i'),
];
In the example above, the PHP attributes equivalent will be:
use Yiisoft\Validator\Validator;
use Yiisoft\Validator\Rule\Date\DateTime;
final class User
{
public function __construct(
#[DateTime(format: 'php:Y-m-d, H:i')]
public string $date,
) }
}
$user = new User(date: '2022-01-01, 23:15');
$validator = (new Validator())->validate($user);
Public Methods
Method Details
| public __construct( string|null $format = null, integer|null $dateType = null, integer|null $timeType = 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, integer|null $messageTimeType = null, string|null $incorrectInputMessage = null, string|null $tooEarlyMessage = null, string|null $tooLateMessage = null, mixed|null $skipOnEmpty = null, boolean $skipOnError = false, Closure|null $when = null ): mixed | ||
| $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 |
| $timeType | integer|null |
Format of the time 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 |
| $messageTimeType | 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 $min. You may use the following placeholders in the message:
|
| $tooLateMessage | string|null |
A message used when the validated date is more than $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 Yiisoft\Validator\SkipOnEmptyInterface. |
| $skipOnError | boolean |
Whether to skip this rule if any of the previous rules gave an error. See \Yiisoft\Validator\Rule\Date\SkipOnErrorInterface. |
| $when | Closure|null |
A callable to define a condition for applying the rule. See Yiisoft\Validator\WhenInterface. |
public function __construct(
?string $format = null,
private readonly ?int $dateType = null,
private readonly ?int $timeType = 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,
private readonly ?int $messageTimeType = 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 getHandler( ): string |
public function getHandler(): string
{
return DateTimeHandler::class;
}
| public getIncorrectInputMessage( ): string|null |
public function getIncorrectInputMessage(): ?string
{
return $this->incorrectInputMessage;
}
Defined in: Yiisoft\Validator\Rule\Date\BaseDate::getMax()
| public getMax( ): DateTimeInterface|integer|string|null |
public function getMax(): DateTimeInterface|int|string|null
{
return $this->max;
}
| public getMessageDateType( ): integer|null |
public function getMessageDateType(): ?int
{
return $this->messageDateType;
}
| public getMessageFormat( ): string|null |
public function getMessageFormat(): ?string
{
return $this->messageFormat;
}
| public getMessageTimeType( ): integer|null |
public function getMessageTimeType(): ?int
{
return $this->messageTimeType;
}
Defined in: Yiisoft\Validator\Rule\Date\BaseDate::getMin()
| public getMin( ): DateTimeInterface|integer|string|null |
public function getMin(): DateTimeInterface|int|string|null
{
return $this->min;
}
Defined in: Yiisoft\Validator\Rule\Date\BaseDate::getName()
| public getName( ): string |
public function getName(): string
{
return 'date';
}
Defined in: Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait::getSkipOnEmpty()
A getter for $skipOnEmpty property.
| public getSkipOnEmpty( ): boolean|callable|null | ||
| return | boolean|callable|null |
A current raw (non-normalized) value. |
|---|---|---|
public function getSkipOnEmpty(): bool|callable|null
{
return $this->skipOnEmpty;
}
| public getTimeZone( ): non-empty-string|null |
public function getTimeZone(): ?string
{
return $this->timeZone;
}
| public getTooEarlyMessage( ): string|null |
public function getTooEarlyMessage(): ?string
{
return $this->tooEarlyMessage;
}
| public getTooLateMessage( ): string|null |
public function getTooLateMessage(): ?string
{
return $this->tooLateMessage;
}
Defined in: Yiisoft\Validator\Rule\Trait\WhenTrait::getWhen()
A getter for $when property.
| public getWhen( ): Closure|null | ||
| 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 shouldSkipOnError( ): boolean | ||
| 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 skipOnEmpty( boolean|callable|null $value ): $this | ||
| $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 skipOnError( boolean $value ): $this | ||
| $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 when( Closure|null $value ): $this | ||
| $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.