FREDERIC LD's avatar

form validation: grouped radio buttons

Hi,

I have a form with several groups of radio buttons. I have left some HTML out to make it easier to read but the main HMTL is down here

<input type="radio" name="subjects[math]" id="subjects[math]['I like it']" value="I like it">
<input type="radio" name="subjects[math]" id="subjects[math]['I dont mind it']" value="I dont mind it">
<input type="radio" name="subjects[math]" id="subjects[math]['not for me']" value="not for me">
<input type="radio" name="subjects[math]" id="subjects[math]['Not applicable']" value="Not applicable">

<input type="radio" name="subjects[english]" id="subjects[english]['I like it']" value="I like it">
<input type="radio" name="subjects[english]" id="subjects[english]['I dont mind it']" value="I dont mind it">
<input type="radio" name="subjects[english]" id="subjects[english]['not for me']" value="not for me">
<input type="radio" name="subjects[english]" id="subjects[english]['Not applicable']" value="Not applicable">

<input type="radio" name="subjects[music]" id="subjects[music]['I like it']" value="I like it">
<input type="radio" name="subjects[music]" id="subjects[music]['I dont mind it']" value="I dont mind it">
<input type="radio" name="subjects[music]" id="subjects[music]['not for me']" value="not for me">
<input type="radio" name="subjects[music]" id="subjects[music]['Not applicable']" value="Not applicable">

My validation is not working

 public function rules()
    {
        return [
            'submit' => 'required',
            'subjects.*' => 'required',
        ];
    }

If I select a couple of values, the validation passes and I get

array:2 [▼
  "submit" => "Next"
  "subjects" => array:2 [▼
    "math" => "I like it"
    "english" => "I like it"
  ]
]

Why is the rest of the checkboxes not validated? I tried several other solutions including one where I replaced the subject(math, english,...) with a number but that's not working either.

What am I missing?

0 likes
7 replies
bugsysha's avatar

When you use input with type radio only selected value is sent to the server. If you want to send multiple then you need checkbox.

FREDERIC LD's avatar

@bugsysha

that's true. I did not quite remember that. What would be the best thing to do for the validation to work in that case. Should run a DB query in the validation rule function to collect all my different subjects and set the required rule dynamically? or is there a better way to do this laravel?

bugsysha's avatar

You do not need validation for the items that were not submitted. Can you explain what are you trying to do?

FREDERIC LD's avatar

@bugsysha

My form display a long list of subject for which a user needs to put a score against. Each subject has radio buttons against it: "I like it", "I don't mid", .... Each of these statements indicates a score and a statement MUST be selected against each subject. To me, the only way I can add a required rule against each subject is to run a query in the validation rule function to collect the list of subject and add the required rule dynamically. I don't think the laravel rules can get this validated using the

subjects.* => 'required'

I should add my list of subjects can change over time. It is not a static list

bugsysha's avatar

Oh, I think I understand now. You are saying that someone can submit the list of answers but leave out the music in this example. Like you said, you can query the database and get all subjects and set the required rule for them. There is nothing wrong with that solution so just use it. I would only advise that for this situation you do not fetch all columns from that table, but only the ones you need.

Snapey's avatar

change the names of your fields, like

<input type="radio" name="subjects[]math" value="I like it">
<input type="radio" name="subjects[]math" value="I dont mind it">
<input type="radio" name="subjects[]math" value="not for me">
<input type="radio" name="subjects[]math" value="Not applicable">

then you can validate just math like

'subjects.*.math' => 'required',

Please or to participate in this conversation.