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 Yiisoft\Validator\Rule\StringValue rule instead.
See also Yiisoft\Validator\Rule\LengthHandler.
Public Methods
Method Details
| public __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 ): mixed | ||
| $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 $min. You may use the following placeholders in the message:
|
| $greaterThanMaxMessage | string |
Error message used when the length of the value is greater than $max. You may use the following placeholders in the message:
|
| $notExactlyMessage | string |
Error message used when the number of items does not equal $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 Yiisoft\Validator\SkipOnEmptyInterface. |
| $skipOnError | boolean |
Whether to skip this rule if any of the previous rules gave an error. See Yiisoft\Validator\SkipOnErrorInterface. |
| $when | Closure|null |
A callable to define a condition for applying the rule. 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 getEncoding( ): string | ||
| 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 $exactly property.
| public getExactly( ): integer|null | ||
| 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 $greaterThanMaxMessage property.
| public getGreaterThanMaxMessage( ): string | ||
| 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 Countable interface.
| public getIncorrectInputMessage( ): string | ||
| return | string |
Error message. |
|---|---|---|
public function getIncorrectInputMessage(): string
{
return $this->incorrectInputMessage;
}
Defined in: Yiisoft\Validator\Rule\Trait\CountableLimitTrait::getLessThanMinMessage()
A getter for $lessThanMinMessage property.
| public getLessThanMinMessage( ): string | ||
| return | string |
Validation error message. |
|---|---|---|
public function getLessThanMinMessage(): string
{
return $this->lessThanMinMessage;
}
Defined in: Yiisoft\Validator\Rule\Trait\CountableLimitTrait::getMax()
A getter for $max property.
| public getMax( ): integer|null | ||
| 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 $min property.
| public getMin( ): integer|null | ||
| 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 $notExactlyMessage property.
| public getNotExactlyMessage( ): string | ||
| return | string |
Validation error message. |
|---|---|---|
public function getNotExactlyMessage(): string
{
return $this->notExactlyMessage;
}
| public getOptions( ): array |
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 getSkipOnEmpty( ): boolean|callable|null | ||
| 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 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.