0 follower

Final Class Yiisoft\Validator\Rule\Each

InheritanceYiisoft\Validator\Rule\Each
ImplementsYiisoft\Validator\AfterInitAttributeEventInterface, Yiisoft\Validator\DumpedRuleInterface, Yiisoft\Validator\PropagateOptionsInterface, Yiisoft\Validator\SkipOnEmptyInterface, Yiisoft\Validator\SkipOnErrorInterface, Yiisoft\Validator\WhenInterface
Uses TraitsYiisoft\Validator\Rule\Trait\SkipOnEmptyTrait, Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait, Yiisoft\Validator\Rule\Trait\WhenTrait

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

Hide inherited properties

Property Type Description Defined By
$stopOnError boolean Yiisoft\Validator\Rule\Each

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Validator\Rule\Each
afterInitAttribute() Yiisoft\Validator\Rule\Each
getHandler() Yiisoft\Validator\Rule\Each
getIncorrectInputKeyMessage() Error message used when validation fails because the validated iterable contains invalid keys. Yiisoft\Validator\Rule\Each
getIncorrectInputMessage() Gets error message used when validation fails because the validated value is not an iterable. Yiisoft\Validator\Rule\Each
getName() Yiisoft\Validator\Rule\Each
getOptions() Yiisoft\Validator\Rule\Each
getRules() Gets a set of rules that needs to be applied to each element of the validated iterable. Yiisoft\Validator\Rule\Each
getSkipOnEmpty() A getter for $skipOnEmpty property. Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait
getWhen() A getter for $when property. Yiisoft\Validator\Rule\Trait\WhenTrait
propagateOptions() Yiisoft\Validator\Rule\Each
shouldSkipOnError() A getter for $skipOnError property. Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait
skipOnEmpty() An immutable setter to change $skipOnEmpty property. Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait
skipOnError() An immutable setter to change $skipOnError property. Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait
when() An immutable setter to change $when property. Yiisoft\Validator\Rule\Trait\WhenTrait

Constants

Hide inherited 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

Hide inherited properties

$stopOnError public property
public boolean $stopOnError false

Method Details

Hide inherited methods

__construct() public method

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:

  • {property}: the translated label of the property being validated.
  • {type}: the type of the value being validated.
$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:

  • {property}: the translated label of the property being validated.
  • {type}: the type of the iterable key being validated.
$skipOnEmpty boolean|callable|null

Whether to skip this Each rule with all defined {@see $rules} if the validated value is empty / not passed. See {@see \Yiisoft\Validator\SkipOnEmptyInterface}.

$skipOnError boolean

Whether to skip this Each rule with all defined {@see $rules} if any of the previous rules gave an error. See {@see \Yiisoft\Validator\SkipOnErrorInterface}.

$when Closure|null

A callable to define a condition for applying this Each rule with all defined {@see $rules}. See {@see \Yiisoft\Validator\WhenInterface}.

$stopOnError boolean

Whether to stop applying rules when an error occurred. Defaults to false.

                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;
}

            
afterInitAttribute() public method

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);
            }
        }
    }
}

            
getHandler() public method

public string getHandler ( )

                public function getHandler(): string
{
    return EachHandler::class;
}

            
getIncorrectInputKeyMessage() public method

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;
}

            
getIncorrectInputMessage() public method

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;
}

            
getName() public method

public string getName ( )

                public function getName(): string
{
    return self::class;
}

            
getOptions() public method

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),
    ];
}

            
getRules() public method

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;
}

            
getSkipOnEmpty() public method

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;
}

            
getWhen() public method

Defined in: Yiisoft\Validator\Rule\Trait\WhenTrait::getWhen()

A getter for $when property.

public Closure|null getWhen ( )
return Closure|null

Current value:

  • null - always apply the validation.
  • callable - apply the validation depending on a return value: true - apply, false - do not apply.

                public function getWhen(): ?Closure
{
    return $this->when;
}

            
propagateOptions() public method

public void propagateOptions ( )

                public function propagateOptions(): void
{
    foreach ($this->rules as $key => $propertyRules) {
        $this->rules[$key] = PropagateOptionsHelper::propagate($this, $propertyRules);
    }
}

            
shouldSkipOnError() public method

Defined in: Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait::shouldSkipOnError()

A getter for $skipOnError property.

public boolean shouldSkipOnError ( )
return boolean

Current value. true means to skip the current rule when the previous one errored and false - do not skip.

                public function shouldSkipOnError(): bool
{
    return $this->skipOnError;
}

            
skipOnEmpty() public method

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;
}

            
skipOnError() public method

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. true means to skip the current rule when the previous one errored and false - do not skip.

return $this

The new instance with a changed value.

                public function skipOnError(bool $value): static
{
    $new = clone $this;
    $new->skipOnError = $value;
    return $new;
}

            
when() public method

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:

  • null - always apply the validation.
  • callable - apply the validation depending on a return value: true - apply, false - do not apply.
return $this

The new instance with a changed value.

                public function when(?Closure $value): static
{
    $new = clone $this;
    $new->when = $value;
    return $new;
}