I have two methods that do practically the same thing; uploading an image to S3. However, they have different arguments and the only different thing about the arguments is the Form Request class. I'm thinking there should be a way to write this so that its one single method.
To expand on @arthurvillar's answer, you can check the request method in your request class if you need to conditionally add rules depending on whether it's a POST or PATCH request.
public function rules()
{
$rules = [
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
];
if ($this->isMethod('POST')) {
$rules['referral'] = 'required';
}
if ($this->isMethod('PATCH')) {
$rules['field'] = 'required';
}
return $rules;
}
Not sure if this is a good practice or not but I've found it useful in a few cases.
It will be really cool if I could have it as just one Form request class but that Form Request class would become messy. Because each of those Form request classes are already adding extra parameters to the request based on if else conditions...
I think I'm gonna go with @biishmar's reply... Thanks guys
@arthurvillar I would really love to see that implementation