0 follower

Final Class Yiisoft\Validator\Rule\Date\Time

InheritanceYiisoft\Validator\Rule\Date\Time » Yiisoft\Validator\Rule\Date\BaseDate
ImplementsYiisoft\Validator\RuleInterface, Yiisoft\Validator\SkipOnEmptyInterface, Yiisoft\Validator\SkipOnErrorInterface, Yiisoft\Validator\WhenInterface
Uses TraitsYiisoft\Validator\Rule\Trait\SkipOnEmptyTrait, Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait, Yiisoft\Validator\Rule\Trait\WhenTrait

Defines validation options to check that the value is a time.

A simple example of how to use it to validate a time:

$rules = [
    'time' => new Yiisoft\Validator\Rule\Date\Time(format: 'php:H:i'),
];

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:H:i')]
        public string $time,
    ) }
}

$user = new User(time: '12:35');

$validator = (new Validator())->validate($user);

See also Yiisoft\Validator\Rule\Date\TimeHandler.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( string|null $format 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 $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 )
$format string|null

The time format that the value being validated should follow. This can be a time pattern as described in the ICU manual.

Alternatively this can be a string prefixed with php: representing a format that can be recognized by the PHP Datetime class. Please refer to {@link https://www.php.net/manual/datetimeimmutable.createfromformat.php} on supported formats.

Here are some example values:

'HH:mm' // time in ICU format
'php:H:i' // time in PHP format
$timeType integer|null

Format of the time determined by one of the IntlDateFormatter constants. It is used when {@see $format} is not set.

$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. UTC, Europe/Berlin or America/Chicago. Refer to the php manual for available timezones.

$locale string|null

Locale to use when formatting or parsing or null to use the value specified in the ini setting intl.default_locale.

$min DateTimeInterface|integer|string|null

Lower limit of the time. Defaults to null, meaning no lower limit. This can be a unix timestamp or a string representing a date value or DateTimeInterface instance.

$max DateTimeInterface|integer|string|null

Upper limit of the time. Defaults to null, meaning no upper limit. This can be a unix timestamp or a string representing a date value or DateTimeInterface instance.

$messageFormat string|null

Format of time that is used in error messages.

$messageTimeType integer|null

One of the IntlDateFormatter constants that used determines time format for error messages used when {@see $messageFormat} is not set.

$incorrectInputMessage string|null

A message used when the validated value is not valid time. You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
$tooEarlyMessage string|null

A message used when the validated time is less than {@see $min}. You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {value}: the validated time.
  • {limit}: expected minimum time.
$tooLateMessage string|null

A message used when the validated time is more than {@see $max}. You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {value}: the validated time.
  • {limit}: expected maximum time.
$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 $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 $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,
    );
}

            
getFormat() public method
public string|null getFormat ( )

                public function getFormat(): ?string
{
    return $this->format;
}

            
getHandler() public method

public string getHandler ( )

                public function getHandler(): string
{
    return TimeHandler::class;
}

            
getIncorrectInputMessage() public method
public string|null getIncorrectInputMessage ( )

                public function getIncorrectInputMessage(): ?string
{
    return $this->incorrectInputMessage;
}

            
getLocale() public method
public string|null getLocale ( )

                public function getLocale(): ?string
{
    return $this->locale;
}

            
getMax() public method
public DateTimeInterface|integer|string|null getMax ( )

                public function getMax(): DateTimeInterface|int|string|null
{
    return $this->max;
}

            
getMessageFormat() public method
public string|null getMessageFormat ( )

                public function getMessageFormat(): ?string
{
    return $this->messageFormat;
}

            
getMessageTimeType() public method

public integer|null getMessageTimeType ( )

                public function getMessageTimeType(): ?int
{
    return $this->messageTimeType;
}

            
getMin() public method
public DateTimeInterface|integer|string|null getMin ( )

                public function getMin(): DateTimeInterface|int|string|null
{
    return $this->min;
}

            
getName() public method
public string getName ( )

                public function getName(): string
{
    return 'date';
}

            
getSkipOnEmpty() public method

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;
}

            
getTimeType() public method

public integer|null getTimeType ( )

                public function getTimeType(): ?int
{
    return $this->timeType;
}

            
getTimeZone() public method
public non-empty-string|null getTimeZone ( )

                public function getTimeZone(): ?string
{
    return $this->timeZone;
}

            
getTooEarlyMessage() public method
public string|null getTooEarlyMessage ( )

                public function getTooEarlyMessage(): ?string
{
    return $this->tooEarlyMessage;
}

            
getTooLateMessage() public method
public string|null getTooLateMessage ( )

                public function getTooLateMessage(): ?string
{
    return $this->tooLateMessage;
}

            
getWhen() public method

Defined in: Yiisoft\Validator\Rule\Trait\WhenTrait::getWhen()

A getter for $when property.

public Closure|null getWhen ( )
return Closure|null

Current value:

  • null - always apply the validation.
  • callable - apply the validation depending on a return value: true - apply, false - do not apply.

                public function getWhen(): ?Closure
{
    return $this->when;
}

            
shouldSkipOnError() public method

Defined in: Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait::shouldSkipOnError()

A getter for $skipOnError property.

public boolean shouldSkipOnError ( )
return boolean

Current value. true means to skip the current rule when the previous one errored and false - do not skip.

                public function shouldSkipOnError(): bool
{
    return $this->skipOnError;
}

            
skipOnEmpty() public method

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;
}

            
skipOnError() public method

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. true means to skip the current rule when the previous one errored and false - do not skip.

return $this

The new instance with a changed value.

                public function skipOnError(bool $value): static
{
    $new = clone $this;
    $new->skipOnError = $value;
    return $new;
}

            
when() public method

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:

  • null - always apply the validation.
  • callable - apply the validation depending on a return value: true - apply, false - do not apply.
return $this

The new instance with a changed value.

                public function when(?Closure $value): static
{
    $new = clone $this;
    $new->when = $value;
    return $new;
}