mehdi
mehdi

Reputation: 119

Laravel Form Request validation by method name

i have a controller with 5 methods store / rename / duplicate / move / delete

comming from deferent Forms with POST.

enter image description here

And i want to use Form Request for Validation like that :

enter image description here

any adea on how to validate all my forms without creating a request form file for each Form.

Upvotes: 0

Views: 952

Answers (1)

Erick Patrick
Erick Patrick

Reputation: 451

You can use $this->route()->getActionName() to the get the current action. ie. MyController@store, MyController@rename, MyController@delete, ...

Then in your SectionRequest you can do something like this:

public function rules(){
    $arr = explode('@', $this->route()->getActionName());
    $method = $arr[1];  // The controller method

    switch ($method) {
       case 'store':
           // do something.
           break;
       case 'rename':
           // do something.
           break;
       case 'delete':
           // .... and so
    }
}

Upvotes: 1

Related Questions