Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Latest commit

 

History

History
60 lines (42 loc) · 1.6 KB

03-rules_each-value.md

File metadata and controls

60 lines (42 loc) · 1.6 KB

EachValue

Validates every element of an array, or object implementing \Traversable, with a given set of rules.

EachValue(
    Validator $validator,
    ?string $message = null
);

Basic Usage

Validator::eachValue(
    Validator::type('int')->range(1, 10)
)->validate([4, 5, 6]); // true

Validator::eachValue(
    Validator::type('int')->range(1, 10)
)->validate([4, 5, 20]); // false

Note

An UnexpectedValueException will be thrown when the input value is not an array or an object implementing \Traversable.

Options

validator

type: Validator required

Validator that will validate each element of an array or object implementing \Traversable.

message

type: ?string default: At key "{{ key }}": {{ message }}

Message that will be shown if at least one input value element is invalid according to the given validator.

// throws: At key 2: The color value should not be blank.
Validator::eachValue(
    Validator::notBlank()
)->assert(['red', 'green', ''], 'color');

The following parameters are available:

Parameter Description
{{ value }} The current invalid value
{{ name }} Name of the invalid value
{{ key }} The key of the invalid iterable element
{{ element }} The value of the invalid iterable element
{{ message }} The rule message of the invalid iterable element

Changelog

  • 0.4.0 Created