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

Input as radio_buttons: `label: false` not working but `label: ""` does. #1788

Open james-em opened 2 years ago

james-em commented 2 years ago

Environment

Current behavior

<%= f.input :change_nature_quebec,
            as: :radio_buttons,
            collection: [[bool_to_text(true), true] ,[bool_to_text(false), false]],
            wrapper: :vertical_collection_inline,
            label: false %>

[BAD]=> Will display "Change nature quebec" as label

<%= f.input :change_nature_quebec,
            as: :radio_buttons,
            collection: [[bool_to_text(true), true] ,[bool_to_text(false), false]],
            wrapper: :vertical_collection_inline,
            label: "" %>
[GOOD]=> Will not display a label

Expected behavior

To never show the label when the value is false.

martinodonnell commented 1 year ago

I came across this issue while trying to set vertical radio button and checkboxes labels to false. When you add label: false, the label does not get removed. You must do legend_tag: false. This isn't included in the readme.

I noticed in the simple forms bootstrap initializers, the vertical collections create the label using a legend_tag wrapper.

config.wrappers :vertical_collection, item_wrapper_class: 'form-check', item_label_class: 'form-check-label', tag: 'fieldset', class: 'mb-3' do |b| 
   b.use :html5 
   b.optional :readonly 
   b.wrapper :legend_tag, tag: 'legend', class: 'col-form-label pt-0' do |ba| 
     ba.use :label_text 
   end 
   b.use :input, class: 'form-check-input', error_class: 'is-invalid', valid_class: 'is-valid' 
   b.use :full_error, wrap_with: { class: 'invalid-feedback d-block' } 
   b.use :hint, wrap_with: { class: 'form-text' } 
 end 

The vertical_collection and vertical_collection_inline templates create labels differently. I would propose updating the file to make them follow the same format.

# vertical input for radio buttons and check boxes 
 config.wrappers :vertical_collection, item_wrapper_class: 'form-check', item_label_class: 'form-check-label', tag: 'fieldset', class: 'mb-3' do |b| 
   b.use :html5 
   b.optional :readonly 
   b.use :label, class: 'col-form-label pt-0' <----- Update to this
   b.use :input, class: 'form-check-input', error_class: 'is-invalid', valid_class: 'is-valid' 
   b.use :full_error, wrap_with: { class: 'invalid-feedback d-block' } 
   b.use :hint, wrap_with: { class: 'form-text' } 
 end 

 # vertical input for inline radio buttons and check boxes 
 config.wrappers :vertical_collection_inline, item_wrapper_class: 'form-check form-check-inline', item_label_class: 'form-check-label', tag: 'fieldset', class: 'mb-3' do |b| 
   b.use :html5 
   b.optional :readonly 
   b.use :label, class: 'col-form-label pt-0' <----- Update to this
   b.use :input, class: 'form-check-input', error_class: 'is-invalid', valid_class: 'is-valid' 
   b.use :full_error, wrap_with: { class: 'invalid-feedback d-block' } 
   b.use :hint, wrap_with: { class: 'form-text' } 
 end 

This means you would update your format to use these.

# Old Syntax
f.input :field, as: :radio_buttons, legend_tag: :false

# Now Syntax
f.input :field, as: :radio_buttons, label: :false