Allows custom errors #761

This commit is contained in:
Mantas 2014-01-26 15:09:19 +02:00
parent ea5ceb7dfa
commit 2ca38cd278
3 changed files with 20 additions and 3 deletions

View File

@ -5,8 +5,12 @@ module SimpleForm
error_text if has_errors?
end
def has_error_in_options?
options[:error] && !options[:error].nil?
end
def has_errors?
object && object.respond_to?(:errors) && errors.present?
(object && object.respond_to?(:errors) && errors.present?) || has_error_in_options?
end
protected
@ -24,7 +28,7 @@ module SimpleForm
end
def errors_on_attribute
object.errors[attribute_name]
has_error_in_options? ? [options[:error]] : object.errors[attribute_name]
end
def errors_on_association

View File

@ -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

View File

@ -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'
@ -123,4 +127,13 @@ class ErrorTest < ActionView::TestCase
assert_select 'span.omg_error', "can't be blank"
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