diff --git a/lib/simple_form/components/errors.rb b/lib/simple_form/components/errors.rb index 70c44c15..32b4d311 100644 --- a/lib/simple_form/components/errors.rb +++ b/lib/simple_form/components/errors.rb @@ -10,7 +10,7 @@ module SimpleForm end def has_errors? - object && object.respond_to?(:errors) && errors.present? + (object && object.respond_to?(:errors) && errors.present?) || has_error_in_options? end protected @@ -36,7 +36,7 @@ module SimpleForm end def errors_on_attribute - object.errors[attribute_name] + has_error_in_options? ? [options[:error]] : object.errors[attribute_name] end def full_errors_on_attribute @@ -50,6 +50,10 @@ module SimpleForm def full_errors_on_association reflection ? object.full_messages_for(reflection.name) : [] end + + def has_error_in_options? + options[:error] && !options[:error].nil? + end end end end diff --git a/lib/simple_form/inputs/base.rb b/lib/simple_form/inputs/base.rb index 510d3766..445b5714 100644 --- a/lib/simple_form/inputs/base.rb +++ b/lib/simple_form/inputs/base.rb @@ -45,7 +45,7 @@ module SimpleForm end # Always enabled. - enable :hint + enable :hint, :error # Usually disabled, needs to be enabled explicitly passing true as option. disable :maxlength, :placeholder, :pattern, :min_max diff --git a/test/form_builder/error_test.rb b/test/form_builder/error_test.rb index bcdf99f8..88695d09 100644 --- a/test/form_builder/error_test.rb +++ b/test/form_builder/error_test.rb @@ -14,6 +14,10 @@ class ErrorTest < ActionView::TestCase end end + def with_custom_error_for(object, *args) + with_form_for(object, *args) + end + test 'error should not generate content for attribute without errors' do with_error_for @user, :active assert_no_select 'span.error' @@ -132,4 +136,13 @@ class ErrorTest < ActionView::TestCase assert_select 'span.error', 'Company must be valid' end end + + # CUSTOM ERRORS + + test 'input with custom error works' do + error_text = "Super User Name! can't be blank" + with_custom_error_for(@user, :name, error: error_text) + + assert_select 'span.error', "#{error_text}" + end end