heartcombo / simple_form

Forms made easy for Rails! It's tied to a simple DSL, with no opinion on markup.
http://blog.plataformatec.com.br/tag/simple_form
MIT License
8.21k stars 1.31k forks source link

Label's for attribute doesn't match the id when nil is given in collection #1840

Open masasakano opened 4 months ago

masasakano commented 4 months ago

Environment

Current behavior

When a collection is explicitly given to radio-buttons and one of the values is nil, the generated for label for the nil one does not match its corresponding id with a difference of a single underscore character. For example,

<%= form.input :my_method, as: :radio_buttons, label: "ABC",
      collection: [['Yes', true], ['Undefined', nil]], checked: nil %>

for Model generates

<fieldset class="form-group radio_buttons model_my_method">
<legend class="col-form-label pt-0">ABC</legend>
<input type="hidden" name="model[my_method]" value="" autocomplete="off" />
<!-- Valid one for "true" -->
<div class="form-check">
  <input class="form-check-input radio_buttons required" type="radio" value="true" name="model[my_method]"
    id="model_my_method_true" />
  <label class="form-check-label collection_radio_buttons"
    for="model_my_method_true">Yes</label>
</div>

<!-- Invalid one for "nil" -->
<div class="form-check">
  <input class="form-check-input radio_buttons required" type="radio" checked="checked" name="model[my_method]"
    id="model_my_method" />
  <label class="form-check-label collection_radio_buttons"
    for="model_my_method_">Undefined</label>
</div>

The second pair of input and label corresponds to the nil-parameter. As you see, the for has a trailing _, whereas that for id does not, and so they are not consistent.

Expected behavior

The label and corresponding ID values must exactly agree.