Add separate span with error message for the current field

pull/3934/head
Oliver Günther 9 years ago
parent 298662f8c0
commit 86a061b6e7
  1. 2
      app/views/common/_error_base.html.erb
  2. 1
      config/locales/en.yml
  3. 13
      lib/tabular_form_builder.rb
  4. 10
      spec/lib/tabular_form_builder_spec.rb

@ -27,7 +27,7 @@ See doc/COPYRIGHT.rdoc for more details.
++#%> ++#%>
<div class="errorExplanation" id="errorExplanation" role="alert"> <div class="errorExplanation" id="errorExplanation" tabindex="0" role="alert">
<% if content_for?(:error_details) %> <% if content_for?(:error_details) %>
<h2><%= error_message %></h2> <h2><%= error_message %></h2>
<%= content_for :error_details %> <%= content_for :error_details %>

@ -453,6 +453,7 @@ en:
errors: errors:
header_invalid_fields: "There were problems with the following fields:" header_invalid_fields: "There were problems with the following fields:"
field_erroneous_label: "This field is invalid: %{full_errors}\nPlease enter a valid value"
activity: activity:
created: "Created: %{title}" created: "Created: %{title}"

@ -200,8 +200,15 @@ class TabularFormBuilder < ActionView::Helpers::FormBuilder
label_options[:class] << 'form--label' label_options[:class] << 'form--label'
has_errors = @object.try(:errors) && @object.errors.include?(field)
label_options[:class] << ' -error' if has_errors content = h(text)
if @object.try(:errors) && @object.errors.include?(field)
label_options[:class] << ' -error'
error_label = I18n.t('errors.field_erroneous_label',
full_errors: @object.errors.full_messages_for(field).join(' '))
content << content_tag('span', error_label, class: 'hidden-for-sighted')
end
label_options[:class] << ' -required' if options.delete(:required) label_options[:class] << ' -required' if options.delete(:required)
label_options[:for] = if options[:for] label_options[:for] = if options[:for]
options[:for] options[:for]
@ -211,7 +218,7 @@ class TabularFormBuilder < ActionView::Helpers::FormBuilder
label_options[:lang] = options[:lang] label_options[:lang] = options[:lang]
label_options.reject! do |_k, v| v.nil? end label_options.reject! do |_k, v| v.nil? end
@template.label(@object_name, field, h(text), label_options) @template.label(@object_name, field, content, label_options)
end end
def element_id(translation_form) def element_id(translation_form)

@ -599,12 +599,20 @@ JJ Abrams</textarea>
context 'with erroneous field' do context 'with erroneous field' do
before do before do
resource.errors.add(:name, :invalid) resource.errors.add(:name, :invalid)
resource.errors.add(:name, :inclusion)
end end
it 'shows an appropriate error label' do it 'shows an appropriate error label' do
expect(output).to have_selector 'label.-error', expect(output).to have_selector 'label.-error',
count: 1, count: 1,
text: User.human_attribute_name(:name) text: 'Name'
end
it 'contains a specific error as a hidden sub-label' do
expect(output).to have_selector 'label.-error span',
count: 1,
text: 'This field is invalid: Name is invalid. ' \
'Name is not set to one of the allowed values.'
end end
end end
end end

Loading…
Cancel
Save