Final Class Yiisoft\Validator\Rule\Length
Defines validation options to check that the value is a string of a certain length.
To check that the value only has the string type, use {@see \Yiisoft\Validator\Rule\StringValue} rule instead.
See also Yiisoft\Validator\Rule\LengthHandler.
Public Methods
Method Details
| public mixed __construct ( integer|null $exactly = null, integer|null $min = null, integer|null $max = null, string $incorrectInputMessage = '{Property} must be a string. {type} given.', string $lessThanMinMessage = '{Property} must contain at least {min, number} {min, plural, one{character} ' . 'other{characters}}.', string $greaterThanMaxMessage = '{Property} must contain at most {max, number} {max, plural, one{character} ' . 'other{characters}}.', string $notExactlyMessage = '{Property} must contain exactly {exactly, number} {exactly, plural, ' . 'one{character} other{characters}}.', string $encoding = 'UTF-8', boolean|callable|null $skipOnEmpty = null, boolean $skipOnError = false, Closure|null $when = null ) | ||
| $exactly | integer|null |
Exact length. |
| $min | integer|null |
Minimum length. |
| $max | integer|null |
Maximum length. |
| $incorrectInputMessage | string |
Error message used when the value is not a string. You may use the following placeholders in the message:
|
| $lessThanMinMessage | string |
Error message used when the length of the value is smaller than {@see $min}. You may use the following placeholders in the message:
|
| $greaterThanMaxMessage | string |
Error message used when the length of the value is greater than {@see $max}. You may use the following placeholders in the message:
|
| $notExactlyMessage | string |
Error message used when the number of items does not equal {@see $exactly}. You may use the following placeholders in the message:
|
| $encoding | string |
The encoding of the string value to be validated (e.g. 'UTF-8'). If this property is not set, application wide encoding will be used. |
| $skipOnEmpty | boolean|callable|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\SkipOnErrorInterface}. |
| $when | Closure|null |
A callable to define a condition for applying the rule. See {@see \Yiisoft\Validator\WhenInterface}. |
public function __construct(
?int $exactly = null,
?int $min = null,
?int $max = null,
private string $incorrectInputMessage = '{Property} must be a string. {type} given.',
string $lessThanMinMessage = '{Property} must contain at least {min, number} {min, plural, one{character} '
. 'other{characters}}.',
string $greaterThanMaxMessage = '{Property} must contain at most {max, number} {max, plural, one{character} '
. 'other{characters}}.',
string $notExactlyMessage = '{Property} must contain exactly {exactly, number} {exactly, plural, '
. 'one{character} other{characters}}.',
private string $encoding = 'UTF-8',
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private ?Closure $when = null,
) {
$this->initCountableLimitProperties(
$min,
$max,
$exactly,
$lessThanMinMessage,
$greaterThanMaxMessage,
$notExactlyMessage,
);
$this->skipOnEmpty = $skipOnEmpty;
}
Get the encoding of the string value to be validated (e.g. 'UTF-8').
If this property is not set, application wide encoding will be used.
| public string getEncoding ( ) | ||
| return | string |
Encoding of the string value to be validated. |
|---|---|---|
public function getEncoding(): string
{
return $this->encoding;
}
Defined in: Yiisoft\Validator\Rule\Trait\CountableLimitTrait::getExactly()
A getter for {@see $exactly} property.
| public integer|null getExactly ( ) | ||
| return | integer|null |
A number representing "exactly" value. |
|---|---|---|
public function getExactly(): ?int
{
return $this->exactly;
}
Defined in: Yiisoft\Validator\Rule\Trait\CountableLimitTrait::getGreaterThanMaxMessage()
A getter for {@see $greaterThanMaxMessage} property.
| public string getGreaterThanMaxMessage ( ) | ||
| return | string |
Validation error message. |
|---|---|---|
public function getGreaterThanMaxMessage(): string
{
return $this->greaterThanMaxMessage;
}
Get error message used when the value is neither an array nor implementing {@see \Countable} interface.
| public string getIncorrectInputMessage ( ) | ||
| return | string |
Error message. |
|---|---|---|
public function getIncorrectInputMessage(): string
{
return $this->incorrectInputMessage;
}
Defined in: Yiisoft\Validator\Rule\Trait\CountableLimitTrait::getLessThanMinMessage()
A getter for {@see $lessThanMinMessage} property.
| public string getLessThanMinMessage ( ) | ||
| return | string |
Validation error message. |
|---|---|---|
public function getLessThanMinMessage(): string
{
return $this->lessThanMinMessage;
}
Defined in: Yiisoft\Validator\Rule\Trait\CountableLimitTrait::getMax()
A getter for {@see $max property}.
| public integer|null getMax ( ) | ||
| return | integer|null |
A number representing maximum boundary. |
|---|---|---|
public function getMax(): ?int
{
return $this->max;
}
Defined in: Yiisoft\Validator\Rule\Trait\CountableLimitTrait::getMin()
A getter for {@see $min} property.
| public integer|null getMin ( ) | ||
| return | integer|null |
A number representing minimum boundary. |
|---|---|---|
public function getMin(): ?int
{
return $this->min;
}
Defined in: Yiisoft\Validator\Rule\Trait\CountableLimitTrait::getNotExactlyMessage()
A getter for {@see $notExactlyMessage} property.
| public string getNotExactlyMessage ( ) | ||
| return | string |
Validation error message. |
|---|---|---|
public function getNotExactlyMessage(): string
{
return $this->notExactlyMessage;
}
| public array getOptions ( ) |
public function getOptions(): array
{
return array_merge($this->getLimitOptions(), [
'incorrectInputMessage' => [
'template' => $this->incorrectInputMessage,
'parameters' => [],
],
'encoding' => $this->encoding,
'skipOnEmpty' => $this->getSkipOnEmptyOption(),
'skipOnError' => $this->skipOnError,
]);
}
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;
}
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.