Attaching the validation rules to the request method on FormRequest
.
Rules will be merged when some requests.
composer require xzxzyzyz/laravel-multi-method-form-request
Using Xzxzyzyz\Laravel\FormRequest\MultiMethodRule
trait in your FormRequest
class.
use Xzxzyzyz\Laravel\FormRequest\MultiMethodRule;
class ExampleRequest extends FormRequest
{
use MultiMethodRule;
// ...
}
Methods corresponding to each request method:
Request Method | Rule Method |
---|---|
GET | getRules() |
POST | createRules() |
PUT | updateRules() |
DELETE | deleteRules() |
And, default rules()
method is always merged.
FormRequest is:
use Xzxzyzyz\Laravel\FormRequest\MultiMethodRule;
class ExampleRequest extends FormRequest
{
use MultiMethodRule;
public function rules()
{
return [
'default' => 'required'
];
}
public function getRules()
{
return [
'default' => 'it_is_get_rule',
'get' => 'required'
];
}
public function createRules()
{
return [
'default' => 'it_is_create_rule',
'post' => 'required'
];
}
public function updateRules()
{
return [
'default' => 'it_is_update_rule',
'put' => 'required'
];
}
public function deleteRules()
{
return [
'default' => 'it_is_delete_rule',
'delete' => 'required'
];
}
}
If GET
request, Return rules is:
name | rules |
---|---|
default | required, it_is_get_rule |
get | required |
If POST
request, Return rules is:
name | rules |
---|---|
default | required, it_is_create_rule |
post | required |
If PUT
request, Return rules is:
name | rules |
---|---|
default | required, it_is_update_rule |
put | required |
If DELETE
request, Return rules is:
name | rules |
---|---|
default | required, it_is_delete_rule |
delete | required |