Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails form_tag does not send params if disabled: true

I have a form_tag in my view and a few text_field_tag nested in it. I do not want the user to edit the text fields so I did :disabled => true.

The problem: If I do :disabled => true the text field value is not passed in params hash, while if I do :readonly => true it does send the values in params hash

Is this expected? If yes then there is no mention of this in the documentation. If not, then should I file a issue in GitHub?

like image 420
Bhavik Shah Avatar asked Jul 21 '15 18:07

Bhavik Shah


2 Answers

Yes, that is the expected behavior. It is probably not mentioned in the Rails documentation because the disabled and readonly control behavior is defined by the W3C spec.

See the W3C documentation for Disabled controls, which states "disabled controls cannot be successful". A "successful" control is defined as being "'valid' for submission." Setting disabled to true on an input field in Rails will not cause it to not be submitted.

Read-only controls, however, can be successful. Therefore, if you wanted the value to be submitted, but not editable, you should use readonly set to true.

like image 139
Dave Powers Avatar answered Sep 22 '22 08:09

Dave Powers


Yes, this is expected, but it has nothing to do with Rails. Per the HTML5 spec, form fields that are disabled are skipped when the form is submitted (see 4.10.22.4 Constructing the form data set).

If you don't want the user to be able to edit the field, but still want its data to be submitted with the form, you should use the readonly attribute, e.g.:

<%= text_field_tag 'year', '2015', readonly: true %> 
like image 24
Jordan Running Avatar answered Sep 23 '22 08:09

Jordan Running