Ad

Is That Possible To Have Multiple Rules Function In Single Request Class?

I have request class like below.

class CategoryRequest extends Request
{
    public function response(array $errors){
        return \Redirect::back()->withErrors($errors)->withInput();
    }

    public function authorize()
    {
        return false;
    }

    public function rules()
    {
        return [
            'Category' => 'required|unique:tblcategory|max:25|min:5'
        ];
    }
}

There is rules function.

In the controller, there are multiple methods that have Request as a Parameter. Most of them vary in the validation point of view. I mean, if I am admin,. I can update 4 fields. If I am manager, I can update 3 and if I am normal user, I can update 2. So validation will change according to roles.

Is that possible to have multiple rules function in Request class ?

Ad

Answer

You can use here any conditions you want, so you you could do something like this:

public function rules()
{
    $rules = [];
    if (Auth::user()->isAdmin()) {
       $rules['Category'] = '...';
    } 
    elseif (Auth::user()->isManager()) {
       $rules['Category'] = '...';
    }        

    return $rules;
}

Of course you need to create isAdmin and isManager in your User model

Ad
source: stackoverflow.com
Ad