I have a Laravel application and am using the Form Request validation.
I have implemented the following:
public function rules(){
return [
'item_name'=>'required',
'item_description'=> 'required',
];
}
In the controller I have the following:
public function storeItem(storeItem $request) {
$validated = $request->validated();
...
...
}
}
This works correctly, but because for certain items not all $request variables are required, I would like to implement a switch statement as follows:
public function rules()
{
$item_type = $this->route('item_type');
switch($item_type) {
case 'type1':
return [
'item_name'=>'required',
'item_description'=> 'required',
];
break;
case 'type2':
return [
'item_name'=>'required',
'item_amount'=> 'required',
'item_favorite'=> 'required',
];
break;
}
}
I'm getting back the following error:
Argument 2 passed to Illuminate\Validation\Factory::make() must be of the type array, null given
This error message seems to suggest I'm not returning an array, but I do have the return statements per switch case so not sure why I see this eeror message.
Any idea how this could be solved? If a switch statement is not the good option, any other idea?
$requestof typestoreItem? - nice_dev'item_type'. You probably want to pass a variable there, else it won't match with any case and defaults to nothing. - nice_dev