Confide custom validator

215 Views Asked by At

I am using confide for user authentication. I wanna use custom rules using laravel's Validator (for firstname and lastname) class as in

$validator = Validator::make($user, $this->rules[$ruleset]);

and check with $validator->passes(). How can I achieve this?

1

There are 1 best solutions below

0
fmgonzalez On

First create your custom validation class. Something like this:

class CustomValidator 
{
    /** 
     * @return bool 
     */
    public function validateFirstname($attribute, $value, $parameters) {
    {
        // Do here your Firstname validation 
        ... 
    }

    /** 
     * @return bool 
     */
    public function validateLastname($attribute, $value, $parameters) {
    {
        // Do here your Lastname validation 
        ... 
    }

}

Then register the new validation rules. For example at bootstrap/start.php file add:

Validator::extend('firstname', 'CustomValidator@validateFirstname');
Validator::extend('lastname', 'CustomValidator@validateLastname');

Then you can use the new rules firtsname and lastname in your models.

I hope it works fine for you.