Add error handling to form labels

This re-enables an `-error` state for labels when the associated
field is erroneous.

Also shows an error icon next to the label and changes the font style.
pull/3934/head
Oliver Günther 9 years ago
parent a13a8922d8
commit ce503e3306
  1. 11
      app/assets/stylesheets/content/_forms.sass
  2. 5
      lib/tabular_form_builder.rb
  3. 14
      spec/lib/tabular_form_builder_spec.rb

@ -342,6 +342,17 @@ fieldset.form--fieldset
@include grid-size(shrink) @include grid-size(shrink)
max-width: none max-width: none
&.-error
@extend .icon-error
color: $content-form-danger-zone-bg-color
font-weight: bold
&::before
@include icon-common
display: inline-block
line-height: $base-line-height
padding-right: 0.325rem
&.-required, &.-required,
.form--field.-required > & .form--field.-required > &
&::after &::after

@ -198,9 +198,10 @@ class TabularFormBuilder < ActionView::Helpers::FormBuilder
id = element_id(translation_form) if translation_form id = element_id(translation_form) if translation_form
# FIXME: reenable the error handling
label_options[:class] << 'error' if false && @object && @object.respond_to?(:errors) && @object.errors[field] # FIXME
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
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]

@ -582,7 +582,7 @@ JJ Abrams</textarea>
end end
end end
context 'with ActiveModel and withouth specified label' do context 'with ActiveModel and without specified label' do
let(:resource) { let(:resource) {
FactoryGirl.build_stubbed(:user, FactoryGirl.build_stubbed(:user,
firstname: 'JJ', firstname: 'JJ',
@ -595,6 +595,18 @@ JJ Abrams</textarea>
it 'uses the human attibute name' do it 'uses the human attibute name' do
expected_label_like(User.human_attribute_name(:name)) expected_label_like(User.human_attribute_name(:name))
end end
context 'with erroneous field' do
before do
resource.errors.add(:name, :invalid)
end
it 'shows an appropriate error label' do
expect(output).to have_selector 'label.-error',
count: 1,
text: User.human_attribute_name(:name)
end
end
end end
context 'when required, with a label specified as symbol' do context 'when required, with a label specified as symbol' do

Loading…
Cancel
Save