Remove explicit required label for impaired users

Denote inputs with `aria-required="required"` instead of a separate span
within the label, which causes double mentions of the field being
required (once through the html5 attribute when the screenreader is
compatible) as well as in the label span.
pull/4095/head
Oliver Günther 9 years ago
parent bbfe941e77
commit d49ba37e0a
  1. 1
      config/locales/en.yml
  2. 43
      lib/tabular_form_builder.rb
  3. 1
      spec/lib/tabular_form_builder_spec.rb

@ -963,7 +963,6 @@ en:
label_feeds_access_key: "RSS access key"
label_feeds_access_key_created_on: "RSS access key created %{value} ago"
label_feeds_access_key_type: "RSS"
label_field_is_required: 'This field is required.'
label_file_added: "File added"
label_file_plural: "Files"
label_filter_add: "Add filter"

@ -40,6 +40,7 @@ class TabularFormBuilder < ActionView::Helpers::FormBuilder
localize_field(field, options, __method__)
else
options[:class] = Array(options[:class]) + [field_css_class(selector)]
merge_required_attributes(options[:required], options)
input_options, label_options = extract_from options
@ -72,10 +73,7 @@ class TabularFormBuilder < ActionView::Helpers::FormBuilder
def select(field, choices, options = {}, html_options = {})
html_options[:class] = Array(html_options[:class]) + %w(form--select)
if options[:required]
html_options[:required] = true
end
merge_required_attributes(options[:required], html_options)
label_for_field(field, options) + container_wrap_field(super, 'select', options)
end
@ -128,6 +126,12 @@ class TabularFormBuilder < ActionView::Helpers::FormBuilder
field_container_wrap_field(ret, options)
end
def merge_required_attributes(required, options=nil)
if required
options.merge!({ required: true, 'aria-required': 'required' })
end
end
def field_container_wrap_field(field_html, options = {})
if options[:no_label]
field_html
@ -183,19 +187,8 @@ class TabularFormBuilder < ActionView::Helpers::FormBuilder
options = options.dup
return ''.html_safe if options.delete(:no_label)
text = if options[:label].is_a?(Symbol)
l(options[:label])
elsif options[:label]
options[:label]
elsif @object.is_a?(ActiveRecord::Base)
@object.class.human_attribute_name(field)
else
l(field)
end
label_options = { class: '',
title: text }
text = get_localized_field(field, options[:label])
label_options = { class: '', title: text }
id = element_id(translation_form) if translation_form
label_options[:class] << 'form--label'
@ -215,10 +208,6 @@ class TabularFormBuilder < ActionView::Helpers::FormBuilder
'*',
class: 'form--label-required',
'aria-hidden': true)
content << content_tag('p',
I18n.t(:label_field_is_required),
class: 'hidden-for-sighted')
end
label_options[:for] = if options[:for]
@ -232,6 +221,18 @@ class TabularFormBuilder < ActionView::Helpers::FormBuilder
@template.label(@object_name, field, content, label_options)
end
def get_localized_field(field, label)
if label.is_a?(Symbol)
l(label)
elsif label
label
elsif @object.is_a?(ActiveRecord::Base)
@object.class.human_attribute_name(field)
else
l(field)
end
end
def element_id(translation_form)
match = /id=\"(?<id>\w+)"/.match(translation_form.hidden_field :id)
match ? match[:id] : nil

@ -557,7 +557,6 @@ JJ Abrams</textarea>
title="#{expected_title}">
#{expected_title}
<span class="form--label-required" aria-hidden="true">*</span>
<p class="hidden-for-sighted">This field is required.</span>
</label>
}).at_path('label')
end

Loading…
Cancel
Save