Skip to content

Latest commit

 

History

History
executable file
·
47 lines (35 loc) · 1.07 KB

17.2 - Password validation.md

File metadata and controls

executable file
·
47 lines (35 loc) · 1.07 KB

17.2 - Password validation example

use KrisKuiper\Validator\Error;
use KrisKuiper\Validator\Validator;

$data = [
    'password' => 'very_strong_password',
    'password_repeat' => 'very_strong_password',
];

$validator = new Validator($data);

$validator
    ->field('password')
    ->required()
    ->isString()
    ->containsDigit()
    ->containsLetter()
    ->containsMixedCase()
    ->containsSymbol()
    ->lengthBetween(8, 50);

$validator
    ->field('password_repeat')
    ->same('password');

//Validation passes
if($validator->passes()) {
    print_r($validator->validatedData()->not('password_repeat')->toArray());
}

//Validation fails
if($validator->fails()) {

    $validator->errors()->each(function(Error $error) {
        print_r($error->getMessage());
    });
}

Go to the previous section.

Go to the next section.