Final Class Yiisoft\Validator\Rule\Each
Allows to define a set of rules for validating each element of an iterable.
An example for simple iterable that can be used to validate RGB color:
$rules = [
new Count(3), // Not required for using with `Each`.
new Each([
new Integer(min: 0, max: 255),
// More rules can be added here.
]),
];
When paired with {@see \Yiisoft\Validator\Rule\Nested} rule, it allows validation of related data:
$coordinateRules = [new Number(min: -10, max: 10)];
$rule = new Each([
new Nested([
'coordinates.x' => $coordinateRules,
'coordinates.y' => $coordinateRules,
]),
]);
It's also possible to use DTO objects with PHP attributes, see {@see \Yiisoft\Validator\DataSet\ObjectDataSet} documentation and guide for details.
Supports propagation of options (see {@see \Yiisoft\Validator\Helper\PropagateOptionsHelper::propagate()} for available options and requirements).
See also Yiisoft\Validator\Rule\EachHandler Corresponding handler performing the actual validation.
Public Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $stopOnError | boolean | Yiisoft\Validator\Rule\Each |
Public Methods
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| PARAMETER_EACH_KEY | 'yii-validator-current-each-key' | A name of parameter string current key in the {@see Each} rule | Yiisoft\Validator\Rule\Each |
Property Details
Method Details
| public mixed __construct ( callable|iterable|object|string $rules = [], string $incorrectInputMessage = '{Property} must be array or iterable. {type} given.', string $incorrectInputKeyMessage = 'Every iterable key of {property} must have an integer or a ' . 'string type. {type} given.', boolean|callable|null $skipOnEmpty = null, boolean $skipOnError = false, Closure|null $when = null, boolean $stopOnError = false ) | ||
| $rules | callable|iterable|object|string |
Rules to apply for each element of the validated iterable. They will be normalized using {@see \Yiisoft\Validator\Helper\RulesNormalizer}. |
| $incorrectInputMessage | string |
Error message used when validation fails because the validated value is not an iterable. You may use the following placeholders in the message:
|
| $incorrectInputKeyMessage | string |
Error message used when validation fails because the validated iterable contains invalid keys. Only integer and string keys are allowed. You may use the following placeholders in the message:
|
| $skipOnEmpty | boolean|callable|null |
Whether to skip this |
| $skipOnError | boolean |
Whether to skip this |
| $when | Closure|null |
A callable to define a condition for applying this |
| $stopOnError | boolean |
Whether to stop applying rules when an error occurred. Defaults to |
public function __construct(
callable|iterable|object|string $rules = [],
private string $incorrectInputMessage = '{Property} must be array or iterable. {type} given.',
private string $incorrectInputKeyMessage = 'Every iterable key of {property} must have an integer or a '
. 'string type. {type} given.',
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private ?Closure $when = null,
public readonly bool $stopOnError = false,
) {
$this->rules = RulesNormalizer::normalize($rules);
$this->skipOnEmpty = $skipOnEmpty;
}
| public void afterInitAttribute ( object $object ) | ||
| $object | object | |
public function afterInitAttribute(object $object): void
{
foreach ($this->rules as $propertyRules) {
foreach ($propertyRules as $rule) {
if ($rule instanceof AfterInitAttributeEventInterface) {
$rule->afterInitAttribute($object);
}
}
}
}
Error message used when validation fails because the validated iterable contains invalid keys.
| public string getIncorrectInputKeyMessage ( ) | ||
| return | string |
Error message / template. |
|---|---|---|
public function getIncorrectInputKeyMessage(): string
{
return $this->incorrectInputKeyMessage;
}
Gets error message used when validation fails because the validated value is not an iterable.
| public string getIncorrectInputMessage ( ) | ||
| return | string |
Error message / template. |
|---|---|---|
public function getIncorrectInputMessage(): string
{
return $this->incorrectInputMessage;
}
| public array getOptions ( ) |
#[ArrayShape([
'incorrectInputMessage' => 'array',
'incorrectInputKeyMessage' => 'array',
'skipOnEmpty' => 'bool',
'skipOnError' => 'bool',
'stopOnError' => 'bool',
'rules' => 'array',
])]
public function getOptions(): array
{
return [
'incorrectInputMessage' => [
'template' => $this->incorrectInputMessage,
'parameters' => [],
],
'incorrectInputKeyMessage' => [
'template' => $this->incorrectInputKeyMessage,
'parameters' => [],
],
'skipOnEmpty' => $this->getSkipOnEmptyOption(),
'skipOnError' => $this->skipOnError,
'stopOnError' => $this->stopOnError,
'rules' => RulesDumper::asArray($this->rules),
];
}
Gets a set of rules that needs to be applied to each element of the validated iterable.
| public array getRules ( ) | ||
| return | array |
A set of rules. |
|---|---|---|
public function getRules(): array
{
return $this->rules;
}
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;
}
| public void propagateOptions ( ) |
public function propagateOptions(): void
{
foreach ($this->rules as $key => $propertyRules) {
$this->rules[$key] = PropagateOptionsHelper::propagate($this, $propertyRules);
}
}
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.