diff --git a/lib/simple_form/components/errors.rb b/lib/simple_form/components/errors.rb index 6d382811..d2d8f689 100644 --- a/lib/simple_form/components/errors.rb +++ b/lib/simple_form/components/errors.rb @@ -10,17 +10,20 @@ module SimpleForm end def has_errors? - has_error_in_options? || (object && object.respond_to?(:errors) && errors.present?) + object && object.respond_to?(:errors) && errors.present? end protected def error_text - "#{html_escape(options[:error_prefix])} #{errors.send(error_method)}".lstrip.html_safe + text = has_error_in_options? ? options[:error] : errors.send(error_method) + + "#{html_escape(options[:error_prefix])} #{text}".lstrip.html_safe end def full_error_text - "#{full_errors.send(error_method)}".html_safe + text = has_error_in_options? ? options[:error] : full_errors.send(error_method) + text.html_safe end def error_method @@ -36,11 +39,11 @@ module SimpleForm end def errors_on_attribute - has_error_in_options? ? [options[:error]] : object.errors[attribute_name] + object.errors[attribute_name] end def full_errors_on_attribute - has_error_in_options? ? [options[:error]] : object.errors.full_messages_for(attribute_name) + object.errors.full_messages_for(attribute_name) end def errors_on_association diff --git a/test/form_builder/error_test.rb b/test/form_builder/error_test.rb index ce262870..b4d1333d 100644 --- a/test/form_builder/error_test.rb +++ b/test/form_builder/error_test.rb @@ -156,6 +156,13 @@ class ErrorTest < ActionView::TestCase assert_select 'span.error', "#{error_text}" end + test 'input with custom error does not generate the error if there is no error on the attribute' do + error_text = "Super User Active! can't be blank" + with_form_for @user, :active, error: error_text + + assert_no_select 'span.error' + end + test 'input with custom error works when using full_error component' do swap_wrapper :default, self.custom_wrapper_with_full_error do error_text = "Super User Name! can't be blank" @@ -164,4 +171,13 @@ class ErrorTest < ActionView::TestCase assert_select 'span.error', "#{error_text}" end end + + test 'input with custom error when using full_error component does not generate the error if there is no error on the attribute' do + swap_wrapper :default, self.custom_wrapper_with_full_error do + error_text = "Super User Active! can't be blank" + with_form_for @user, :active, error: error_text + + assert_no_select 'span.error' + end + end end