Repository navigation
Description
Steps to reproduce
Follow the example given in the Rails API Documentation on FormOptionsHelper:
class Post < ActiveRecord::Base
has_and_belongs_to_many :authors
end
class Author < ActiveRecord::Base
has_and_belongs_to_many :posts
def name_with_initial
"#{first_name.first}. #{last_name}"
end
endand use the collection_check_boxes helper or collection_radio_buttons helper inside a form_with block in app/views/posts/_form.html.erb:
<%= form_with(model: post, local: true) do |form| %>
<%# ... %>
<%= form.collection_check_boxes :author_ids, Author.all, :id, :name_with_initial %>
<%# ... %>
<% end %>You can find an example project at https://github.com/sebroeder/demo_form_with_missing_ids_on_check_boxes_and_radio_buttons
Expected behavior
One should be able to toggle the check boxes (or radio buttons) by clicking on their labels, not only by clicking on the check box or radio button itself. Screenreaders should be able to read the labels that are associated to an input element.
This is even mentioned in the official Action View Form Helpers Guide:
Always use labels for checkbox and radio buttons. They associate text with a specific option and, by expanding the clickable region, make it easier for users to click the inputs.
To make this behavior possible, the generated input elements should have autogenerated id attributes that match the ones the label’s for attribute uses:
<input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="1" checked="checked" />
<label for="post_author_ids_1">D. Heinemeier Hansson</label>
<input id="post_author_ids_2" name="post[author_ids][]" type="checkbox" value="2" />
<label for="post_author_ids_2">D. Thomas</label>
<input id="post_author_ids_3" name="post[author_ids][]" type="checkbox" value="3" />
<label for="post_author_ids_3">M. Clark</label>
<input name="post[author_ids][]" type="hidden" value="" />Strangely, there are even tests that should cover the creation of ids.
Actual behavior
The collection_check_boxes and collection_radio_buttons helpers, when used inside a form_with block, do not auto-generate id attributes for any form elements, including the check box and radio button input elements:
<input type="hidden" name="post[author_ids][]" value="" />
<input type="checkbox" value="1" name="post[author_ids][]" />
<label for="post_author_ids_1">D. Heinemeier Hansson</label>
<input type="checkbox" value="2" name="post[author_ids][]" />
<label for="post_author_ids_2">D. Thomas</label>
<input type="checkbox" value="3" name="post[author_ids][]" />
<label for="post_author_ids_3">M. Clark</label>While this is in general the intended behavior, as the commit message of a6d065e and for example this discussion show, it seems like the cases of collection_check_boxes and collection_radio_buttons helpers have not been considered.
In both helpers Rails generates pairs of labels and input elements and the generated HTML could be considered broken, with the unintended side effects of labels that do not toggle its input fields and broken Screenreader support, because the id attributes referenced by the labels do not exist. This is different from other cases, where the developer generates the labels and form fields separately and is responsible for providing the appropriate id attributes.
I could not find a way to override the :skip_default_ids option only for the collection_check_boxes and collection_radio_buttons helpers.
This issues goes away when one uses the older form_for(@post) syntax instead of form_with.
System configuration
Rails version: 5.1.0
Ruby version: 2.4.1
Activity
morgoth commentedon May 15, 2017
Actually, there is no id on any input when using
form_withAlso, it's not possible to get autogenerate behavior because of hardcoded option https://github.com/rails/rails/blob/v5.1.1/actionview/lib/action_view/helpers/form_helper.rb#L717
It makes forms less nice, because of it, as one have to write something like:
It also looks inconsistent, as the
labelmethod still generates attributeforout of the box.@kaspth What was the reason for this change?
hansondr commentedon May 24, 2017
It seems like removing labels generated
forattribute would be most in line with the decision to remove generatedids whenoptions[:skip_default_ids]is turned on. Forcing developers to explicitly tielabels to other form elements e.g.Personally, I would like
:skip_default_idsas an actual option (defaulted as on) so it could be turned off for cookie cutter forms.bluz71 commentedon May 31, 2017
The skipping of
ids in form_with also breaks certain Capybara tests.Previously one could
fill_inbased upon label text such as:Title being the visible value of the label above the input field. Under the covers the
form_forwould auto-create a labelforthat matched up with the auto-generatedidof the associated input.No more default id breaks the linkage between the label and the input, hence breaks many a test, including many of my own.
Note, I am not suggesting any behaviour change to
form_with.However, I do agree with @hansondr, making
:skip_default_idsavailable inform_withoptions would be of great benefit. Obviously defaulted to on as now, but allowing users to getids again if they want.vinc commentedon May 31, 2017
At the moment
<label>implicitely generate aforattribute, but<input>doesn't generate the correspondingidattribute. This breaks capybara as @bluz71 suggest, and also accessibility.To fix this we indeed have to set both explicitely, which is quite verbose and unexpected.
In my opinion the least surprising option would be for
form_withto act likeform_forand add anidattribute to<input>to make it work with<label>.Should have the same output as:
As mentionned previously, adding
:skip_default_idstoform_withis also an acceptable alternative to get the exepected behaviour:bluz71 commentedon Jun 12, 2017
The recent merges directly reference the issue noted in the first post in regards to checkbox and radiobuttons. Nice, good work.
But it looks like it doesn't address the lost linkage between Labels and Text Inputs/Areas as noted by @hansondr, myself and @vinc (just above), or am I missing something? Should we create new issue for those cases? It would make sense for all controls to have the same
skip_default_idsbehaviour, right?