I am working on the Laravel Validation code. In my code, I used the radio button for three options like approve, reject, and return. In the Remarks textbox, validation depends on the Radio button values. Without choosing any options, the validation is working fine, like "Please choose any one." If I choose the Approve option, then the Remarks textbox validation shows an error like "Please enter remarks for your approval". If I choose the Reject Option, it shows an error like "Please enter remarks for rejection."
<input type="radio" id="radiooption" name="approve" value="-1">
<input type="radio" id="radiooption" name="reject" value="R">
<input type="radio" id="radiooption" name="return" value="A">
<input type="text" value = "Remarks" name = "remarks" >
$validated = $request->validate([
'radiooption' => ['required'],
'remarks' => ['required_if:radiooption',-1,'A','R']
]
[
'radiooption.required' => 'Please choose one of the action.',
'remarks.required_if' => [
'value' => [
'A' => 'Please enter remarks for your approval',
'-1' => 'Please enter remarks for your Accept',
'R' => "Please enter remarks for your Reject"
],]
Above is not working, It shows error like Validation rule required_if requires at least 2 parameters. Please support me to solve that issue
2
Answers
The syntax for your validation seems incorrect, required_if can take multiple values to check against the field. While the usage of
|
seems incorrect, normally the values would be separated in the string like thisrequired|string
, you are doing it outside the string.Like mentioned in the comments above, You’ve mixed up your attributes on your input tags. Each radio button should have the same name (
radiooption
in this case). Where as the id’s should be unique.Once that is fixed, using @mrhn’s answer above will also be required.