Skip to content

Missing ids when using collection_check_boxes or collection_radio_buttons inside form_with  #29014

Closed

Description

@sebroeder

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
end

and 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

  1. morgoth commented on May 15, 2017

    @morgoth
    Member

    Actually, there is no id on any input when usingform_with
    Also, 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:

    <%= f.label :city %>
    <%= f.text_field :city, id: "object_class_name_city" %>
    

    It also looks inconsistent, as the label method still generates attribute for out of the box.

    @kaspth What was the reason for this change?

  2. hansondr commented on May 24, 2017

    @hansondr

    It seems like removing labels generated for attribute would be most in line with the decision to remove generated ids when options[:skip_default_ids] is turned on. Forcing developers to explicitly tie labels to other form elements e.g.

    <%= form_with model: Post.new do |form| %>
      <%= form.label :title, for: :post_title %>
      <%= form.text_field :title, id: :post_title %>
    <% end %>

    Personally, I would like :skip_default_ids as an actual option (defaulted as on) so it could be turned off for cookie cutter forms.

  3. bluz71 commented on May 31, 2017

    @bluz71

    The skipping of ids in form_with also breaks certain Capybara tests.

    Previously one could fill_in based upon label text such as:

    fill_in "Title", with: "XYZ"
    

    Title being the visible value of the label above the input field. Under the covers the form_for would auto-create a label for that matched up with the auto-generated id of 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_ids available in form_with options would be of great benefit. Obviously defaulted to on as now, but allowing users to get ids again if they want.

  4. vinc commented on May 31, 2017

    @vinc

    At the moment <label> implicitely generate a for attribute, but <input> doesn't generate the corresponding id attribute. 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_with to act like form_for and add an id attribute to <input> to make it work with <label>.

    <%= form_with(model: user) do |form| %>
      <%= form.label :name %>
      <%= form.text_field :name %>
    <% end %>

    Should have the same output as:

    <%= form_for(user) do |form| %>
      <%= form.label :name %>
      <%= form.text_field :name %>
    <% end %>

    As mentionned previously, adding :skip_default_ids to form_with is also an acceptable alternative to get the exepected behaviour:

    <%= form_with(model: service, skip_default_ids: false) do |form| %>
      <%= form.label :name %>
      <%= form.text_field :name %>
    <% end %>
  5. bluz71 commented on Jun 12, 2017

    @bluz71

    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_ids behaviour, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions