-1

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="approve" name="radiooption" value="-1">
<input type="radio" id="reject" name="radiooption" value="R">
<input type="radio" id="return" name="radiooption" 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' => 'Please enter reason(s) to applicant :values',

]

Above is not working, It shows error like Validation rule required_if requires at least 2 parameters. Please support me to solve that issue

3
  • The code is not working Commented Sep 8, 2023 at 13:05
  • How should it work instead? Please post that information in your question. Commented Sep 8, 2023 at 13:10
  • 1
    Sidenote, you're using id="radiooption" 3 times, but ids need to be unique, or use class="radiooption" instead. No effect on your current issue (which is a syntax error on usage of required_if: laravel.com/docs/10.x/validation#rule-required-if), but if you ever tried to target document.getElementById('radiooption'), you'd run into issues. Commented Sep 8, 2023 at 16:03

1 Answer 1

1

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 this required|string, you are doing it outside the string.

 'remarks' => ['required_if:radiooption,1,2,3']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.