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

Disabled radio_buttons field doesn't post the correct value. #1843

Closed bruno-talkshoplive closed 2 months ago

bruno-talkshoplive commented 3 months ago

Precheck

Environment

Current behavior

When a radio_buttons field is disabled the field value is not being posted.

 <%= f.input :generation_type, as: :radio_buttons, 
                      collection: GenerationTypeEnum.to_a, 
                      disabled: !@bill.new_record? %>

Param request

  "bill"=>{"bill_type"=>"1", "generation_type"=>"" }

The form generates a hidden field, but it doesn't set the value to it.

image

Expected behavior

It should continue posting the param, I think the problem is caused by when the field is disabled the hidden field start to be preferred.

Workaround

 <%= f.input :generation_type, as: :radio_buttons, 
                      collection: GenerationTypeEnum.to_a, 
                      disabled: !@bill.new_record?, 
                      include_hidden: false %>
nashby commented 2 months ago

Hey @bruno-talkshoplive! The thing is that disabled inputs should not be sent to the server as per HTML spec: https://www.w3.org/TR/REC-html40-971218/interact/forms.html#h-17.12.1

When set, the disabled attribute has the following effects on an element:

Disabled controls do not receive focus. Disabled controls are skipped in tabbing navigation. Disabled controls cannot be successful.

What you probably want is readonly attribute if you want to keep sending it but make it not editable.

Hidden input is a bit different thing and you can read this discussion to get more info https://github.com/heartcombo/simple_form/issues/1728