Rails form_for required on select field not working

26 Views Asked by At

I'm using rails 6.1 and 'ruby 3.1.1`

In a form, using form_for, I have a select field that is using :required => true, but it is not working. The page submits without prompting me to complete that field. Validations in the model force the form to be rendered again, but I was hoping to avoid that step. I think it makes the user experience a bit easier.

I have seen other threads here on Stackoverflow about this, but what solves a couple of them is not working for me. Sigh!

<tr>
  <td>Badge Category:</td>
  <td><%= f.select :category, options_for_select(LivingMuayThai::Badge::BADGE_CATEGORIES, @badge.category), 
                   :prompt => "Select a Category", html: {:required => true} %></td>
</tr>

I've also tried:

{:required => true} # no html:
:required => true

I thought maybe the select prompt was being seen as an option that was selected, but I removed it and it still does not work.

Am I missing something obvious? Thanks,

Edited this post to add the html output on the form
HTML Outputed:

<select id="living_muay_thai_badge_category" name="living_muay_thai_badge[category]">
  <option value="">Select a Category</option>
  <option value="Attendance">Attendance</option>
  <option value="Boxing">Boxing</option>
  <option value="Clinch">Clinch</option>
  ...
</select>
1

There are 1 best solutions below

2
Sebastian Scholl On

Two questions:

  1. What does your rendered html look like?
  2. Are you not providing a default option with @badge.category?