Ted
Ted

Reputation: 4166

Use 2 different FormRequests in a single controller

Is it possible to use 2 different FormRequest validations in a single Controller, one for store and the other for index, how?

I used method() to returns different validations from rules(), ex:

public function rules()
{

    if($this->method() == 'GET')
    {
        return [
            'customer' => 'required|numeric',
        ];
    }

    if($this->method() == 'POST')
    {
        return [
            'author' => 'required|numeric',
        ];
    }
}

but looks very uncomfortable

Upvotes: 0

Views: 67

Answers (1)

Mike Ross
Mike Ross

Reputation: 2972

You can use 2 different Formrequest in one controller.

I do it as following

class PostController extends Controller
{

    public function index(ManagePostRequest $request){
       // your code goes here
    }

    public function create(CreatePostRequest $request){
       // your code goes here
    }

    public function store(StorePostRequest $request){
       // your code goes here
    }
}

So according to the method you can have different rules in form request. Also you can use them for authorize the method.

Hope this is what you were asking as the question was a little unclear to me.

Upvotes: 1

Related Questions