How to Use the Same Validation Form Request for both Create and Update in Laravel
In this lesson, we will see how to use the same validation form request for both create and update in Laravel, sometimes you do not want to make two different form validation requests one for create and one for update and you want to use only one for both actions.
Use the Same Validation Form Request for both Create and Update in Laravel
So to use the same validation form request for both creating and updating in Laravel we check if the method is 'POST' which means that we are creating then we add the rules for the create action else which means that we are updating then we provide the rules for the update action see the code below:
public function rules()
{
return [
'title' => $this->getMethod() == 'POST' ? 'required|string' : 'string'
]
}