Final Class Yiisoft\Validator\Rule\In
Defines validation options to check that the value is one of the values provided in $values.
If the \Yiisoft\Validator\Rule\In::$not is set, the validation logic is inverted and the rule will ensure that the value is NOT one of them.
In case of the validated value being a list, the order of values is important.
Nested arrays are supported too in both \Yiisoft\Validator\Rule\values argument and in the validated value (the order of values in lists must match, the order of keys in associative arrays is not important).
If the validated value is a set, use Yiisoft\Validator\Rule\Subset instead.
See also Yiisoft\Validator\Rule\InHandler.
Public Methods
Method Details
| public __construct( iterable $values, boolean $strict = false, boolean $not = false, string $message = '{Property} is not in the list of acceptable values.', boolean|callable|null $skipOnEmpty = null, boolean $skipOnError = false, Closure|null $when = null ): mixed | ||
| $values | iterable |
A set of values to check against. Nested arrays are supported too (the order of values in lists must match, the order of keys in associative arrays is not important). |
| $strict | boolean |
Whether the comparison to each value in the set is strict:
Defaults to |
| $not | boolean |
Whether to invert the validation logic. Defaults to |
| $message | string |
Error message when the value is not in a set of value. 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(
private iterable $values,
private bool $strict = false,
private bool $not = false,
private string $message = '{Property} is not in the list of acceptable values.',
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private ?Closure $when = null,
) {
$this->skipOnEmpty = $skipOnEmpty;
}
Get error message when the value is not in a set of $values.
| public getMessage( ): string | ||
| return | string |
Error message. |
|---|---|---|
public function getMessage(): string
{
return $this->message;
}
| public getOptions( ): array |
public function getOptions(): array
{
return [
'values' => $this->values,
'strict' => $this->strict,
'not' => $this->not,
'message' => [
'template' => $this->message,
'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;
}
Get a set of values to check against.
| public getValues( ): iterable | ||
| return | iterable |
A set of values. |
|---|---|---|
public function getValues(): iterable
{
return $this->values;
}
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;
}
Whether to invert the validation logic. Defaults to false. If set to true, the value must NOT
be among the list of $values.
| public isNot( ): boolean | ||
| return | boolean |
Whether to invert the validation logic. |
|---|---|---|
public function isNot(): bool
{
return $this->not;
}
Whether the comparison is strict (both type and value must be the same).
| public isStrict( ): boolean | ||
| return | boolean |
Whether the comparison is strict. |
|---|---|---|
public function isStrict(): bool
{
return $this->strict;
}
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.