Confide custom validator

半世苍凉 提交于 2019-12-12 06:55:44

问题


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:


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.



来源:https://stackoverflow.com/questions/26788297/confide-custom-validator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!