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 {@see \
See also Yiisoft\
Public Methods
Method Details
| public mixed __construct ( integer|null $exactly = null, integer|null $min = null, integer|null $max = null, string $incorrectInputMessage = '{Property} must be an array or implement \\ | ||
| $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 {@see $exactly}. See {@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 {@see $exactly}. See {@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 {@see \ You may use the following placeholders in the message:
|
| $lessThanMinMessage | string |
Error message used when the number of items is smaller than {@see $min}. You may use the following placeholders in the message:
|
| $greaterThanMaxMessage | string |
Error message used when the number of items 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:
|
| $skipOnEmpty | boolean|callable|null |
Whether to skip this rule if the value validated is empty.
See {@see \ |
| $skipOnError | boolean |
Whether to skip this rule if any of the previous rules gave an error.
See {@see \ |
| $when | Closure|null |
A callable to define a condition for applying the rule.
See {@see \ |
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\
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\
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 an object implementing {@see Countable} interface.
| public string getIncorrectInputMessage ( ) | ||
| return | string |
Error message. |
|---|---|---|
public function getIncorrectInputMessage(): string
{
return $this->incorrectInputMessage;
}
Defined in:
Yiisoft\
A getter for {@see $lessThanMinMessage} property.
| public string getLessThanMinMessage ( ) | ||
| return | string |
Validation error message. |
|---|---|---|
public function getLessThanMinMessage(): string
{
return $this->lessThanMinMessage;
}
Defined in:
Yiisoft\
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\
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\
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->getIncorrectInputMessage(),
'parameters' => [],
],
'skipOnEmpty' => $this->getSkipOnEmptyOption(),
'skipOnError' => $this->skipOnError,
]);
}
Defined in:
Yiisoft\
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\
A getter for $when property.
| public Closure|null getWhen ( ) | ||
| return | Closure|null |
Current value:
|
|---|---|---|
public function getWhen(): ?Closure
{
return $this->when;
}
Defined in:
Yiisoft\
A getter for $skipOnError property.
| public boolean shouldSkipOnError ( ) | ||
| return | boolean |
Current value. |
|---|---|---|
public function shouldSkipOnError(): bool
{
return $this->skipOnError;
}
Defined in:
Yiisoft\
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\
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\
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;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.