I am trying to access a data called "section" in my rules() function inside my FormRequest Validator Class. What I am trying to do is check what is the value of "section" and return different rules. But apparently, I am not able to access the value of the data/payload.
I have checked the answer from here, but it didn't work for me : Laravel Form Request Validation with switch statement
Here is my code :
FormController.php
class FormController extends Controller
{
public function verify(DynamicFormRequest $request , $section)
{
return response()->json(['success' => 'everything is ok']);
}
}
DynamicFormRequest.php
class DynamicFormRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
error_log($this->request->get('section'));//retruns null and depricated
error_log($this->request->input('section')); //this is absolutely wronge but , i tried it anyways
error_log($this->request->section); //retruns null
error_log($this->section); //retruns null
switch ($this->request->get('section')) {
case '1':
return [
'item_name' => 'required',
];
break;
case 'Type2':
return [
'item_favorite' => 'required',
];
break;
}
}
}
Please help to make me understand whats wronge