diff --git a/lib/generators/simple_form/templates/simple_form.rb b/lib/generators/simple_form/templates/simple_form.rb index 27a741ec..499c6ace 100644 --- a/lib/generators/simple_form/templates/simple_form.rb +++ b/lib/generators/simple_form/templates/simple_form.rb @@ -16,6 +16,9 @@ SimpleForm.setup do |config| # You can wrap all inputs in a pre-defined tag. # config.wrapper_tag = :div + # CSS class to add to the wrapper if the field has errors + # config.wrapper_errors_class = :fieldWithErrors + # How the label text should be generated altogether with the required text. # config.label_text = lambda { |label, required| "#{required} #{label}" } diff --git a/lib/simple_form.rb b/lib/simple_form.rb index 5fe7ac83..893566c1 100644 --- a/lib/simple_form.rb +++ b/lib/simple_form.rb @@ -33,6 +33,9 @@ module SimpleForm mattr_accessor :wrapper_tag @@wrapper_tag = :div + mattr_accessor :wrapper_errors_class + @@wrapper_errors_class = :fieldWithErrors + # How the label text should be generated altogether with the required text. mattr_accessor :label_text @@label_text = lambda { |label, required| "#{required} #{label}" } diff --git a/lib/simple_form/components/wrapper.rb b/lib/simple_form/components/wrapper.rb index 447a1aa5..2b75385a 100644 --- a/lib/simple_form/components/wrapper.rb +++ b/lib/simple_form/components/wrapper.rb @@ -13,9 +13,15 @@ module SimpleForm options[:wrapper_tag] || SimpleForm.wrapper_tag end + def errors_class + options[:wrapper_errors_class] || SimpleForm.wrapper_errors_class + end + def wrapper_html_options - html_options_for(:wrapper, "input", input_type, required_class) + css_classes = ["input", input_type, required_class] + css_classes << errors_class if object && errors.present? + html_options_for(:wrapper, css_classes) end end end -end \ No newline at end of file +end diff --git a/test/components/wrapper_test.rb b/test/components/wrapper_test.rb new file mode 100644 index 00000000..7002d491 --- /dev/null +++ b/test/components/wrapper_test.rb @@ -0,0 +1,25 @@ +require 'test_helper' + +class WrapperTest < ActionView::TestCase + def with_error_for(object, attribute_name, options={}, &block) + concat(simple_form_for object do |f| + f.options = options + f.input attribute_name + end) + end + + test 'wrapper should not have error class for attribute without errors' do + with_error_for @user, :active + assert_no_select 'div.fieldWithErrors' + end + + test 'wrapper should not have error class when object is not present' do + with_error_for :project, :name + assert_no_select 'div.fieldWithErrors' + end + + test 'wrapper should add error class for attribute with errors' do + with_error_for @user, :name + assert_select 'div.fieldWithErrors' + end +end diff --git a/test/form_builder_test.rb b/test/form_builder_test.rb index 14e8329f..9570be13 100644 --- a/test/form_builder_test.rb +++ b/test/form_builder_test.rb @@ -288,6 +288,11 @@ class FormBuilderTest < ActionView::TestCase assert_select 'form b.required.string' end + test 'builder wrapping tag adds erros class for attribute with errors' do + with_form_for @user, :name + assert_select 'form div.input.required.string.fieldWithErrors' + end + # WITHOUT OBJECT test 'builder should generate properly when object is not present' do with_form_for :project, :name