By using the field()
method, you can add one or more field names for validation.
Add one or combine multiple fields to attach rules and more:
use KrisKuiper\Validator\Validator;
$input = ['username' => '', 'password' => '', 'email' =>> ''];
$validator = new Validator($input);
//You can add a single field name
$validator->field('username')->required();
//Or add multiple field names
$validator->field('username', 'password', 'email')->required();
You can also use wildcards:
$data = [
'programmers' => [
'name' => 'Morris',
'email' => '[email protected]'
],
'developers' => [
'name' => 'Smith',
'email' => '[email protected]'
]
];
$validator = new Validator($data);
//Select every email field within an array using wildcards and attach different rules
$validator->field('*.email')->required()->email();
You can also validate an array
of fields. You can validate all the single elements in an array
by using a *
character. Although it's not mandatory, but recommended to validate the array as an array
as well for proper error messages.
$input = [
'emails' => [
'[email protected]',
'[email protected]',
'[email protected]',
]
];
$validator = new Validator($input);
$validator->field('email')->isArray(); //Check if the value is in array (recommended)
$validator->field('email.*')->email(); //Each value should be a valid email address
Note: Rules are executed in the order they are defined.
Go to the previous section.
Go to the next section.