Skip to content

Latest commit

 

History

History
executable file
·
86 lines (61 loc) · 1.57 KB

10.2 - Filtering validated data.md

File metadata and controls

executable file
·
86 lines (61 loc) · 1.57 KB

10.2 - Filter validated data

You can include or exclude fields from the validated array by using the not(), only() and pluck() methods:

use KrisKuiper\Validator\Validator;

$data = [
    'username' => 'Morris', 
    'email' => '[email protected]',
    'password' => '123',
    'interest' => [
        ['title' => 'programming'],
        ['title' => 'coding']
    ]
];

$validator = new Validator($data);

$validator
    ->field('username', 'email', 'password', 'interest.*.title')
    ->required();

$validator->execute();

Only method

Include field names using only() method:

$validator
    ->validatedData()
    ->only('username', 'email')
    ->toArray();

This will return:

array(
    'username' => 'Morris', 
    'email' => '[email protected]'
);

Not method

Exclude field names using not() method:

$validator
    ->validatedData()
    ->not('username', 'email', 'interest')
    ->toArray();

This will return:

array('password' => '123');

Pluck method

Extract multiple columns in key-pair value using pluck() method:

$validator
    ->validatedData()
    ->pluck('interest.*.title')
    ->toArray();

This will return:

array('programming', 'coding');

Go to the previous section.

Go to the next section.