Final Class Yiisoft\Validator\Rule\Count
Defines validation options to check that the value contains certain number of items.
Can be applied to arrays or classes implementing Countable interface.
See also Yiisoft\Validator\Rule\CountHandler.
Public Methods
Method Details
| public __construct( integer|null $exactly = null, integer|null $min = null, integer|null $max = null, string $incorrectInputMessage = '{Property} must be an array or implement \\Countable interface. ' . '{type} given.', string $lessThanMinMessage = '{Property} must contain at least {min, number} {min, plural, one{item} ' . 'other{items}}.', string $greaterThanMaxMessage = '{Property} must contain at most {max, number} {max, plural, one{item} ' . 'other{items}}.', string $notExactlyMessage = '{Property} must contain exactly {exactly, number} {exactly, plural, one{item} ' . 'other{items}}.', boolean|callable|null $skipOnEmpty = null, boolean $skipOnError = false, Closure|null $when = null ): mixed | ||
| $exactly | integer|null |
Exact number of items. |
| $min | integer|null |
Minimum number of items. null means no minimum number limit. Can't be combined with $exactly. See $lessThanMinMessage for the customized message for a value with too few items. |
| $max | integer|null |
Maximum number of items. null means no maximum number limit. Can't be combined with $exactly. See $greaterThanMaxMessage for the customized message for a value with too many items. |
| $incorrectInputMessage | string |
Error message used when the value is neither an array nor an object implementing Countable interface. You may use the following placeholders in the message:
|
| $lessThanMinMessage | string |
Error message used when the number of items is smaller than $min. You may use the following placeholders in the message:
|
| $greaterThanMaxMessage | string |
Error message used when the number of items 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:
|
| $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 an array or implement \Countable interface. '
. '{type} given.',
string $lessThanMinMessage = '{Property} must contain at least {min, number} {min, plural, one{item} '
. 'other{items}}.',
string $greaterThanMaxMessage = '{Property} must contain at most {max, number} {max, plural, one{item} '
. 'other{items}}.',
string $notExactlyMessage = '{Property} must contain exactly {exactly, number} {exactly, plural, one{item} '
. 'other{items}}.',
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private ?Closure $when = null,
) {
$this->initCountableLimitProperties(
$min,
$max,
$exactly,
$lessThanMinMessage,
$greaterThanMaxMessage,
$notExactlyMessage,
);
$this->skipOnEmpty = $skipOnEmpty;
}
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 an object 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->getIncorrectInputMessage(),
'parameters' => [],
],
'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.