Check if error is a string to consider it as custom error

This commit is contained in:
Rafael Mendonça França 2014-04-03 18:24:17 -03:00
parent 7141f1b0ba
commit ed7dda96fe
2 changed files with 10 additions and 4 deletions

View File

@ -16,13 +16,13 @@ module SimpleForm
protected protected
def error_text def error_text
text = has_error_in_options? ? options[:error] : errors.send(error_method) text = has_custom_error? ? options[:error] : errors.send(error_method)
"#{html_escape(options[:error_prefix])} #{text}".lstrip.html_safe "#{html_escape(options[:error_prefix])} #{text}".lstrip.html_safe
end end
def full_error_text def full_error_text
text = has_error_in_options? ? options[:error] : full_errors.send(error_method) text = has_custom_error? ? options[:error] : full_errors.send(error_method)
text.html_safe text.html_safe
end end
@ -54,8 +54,8 @@ module SimpleForm
reflection ? object.full_messages_for(reflection.name) : [] reflection ? object.full_messages_for(reflection.name) : []
end end
def has_error_in_options? def has_custom_error?
options[:error] && !options[:error].nil? options[:error].is_a?(String)
end end
end end
end end

View File

@ -156,6 +156,12 @@ class ErrorTest < ActionView::TestCase
assert_select 'span.error', error_text assert_select 'span.error', error_text
end end
test 'input with custom error works 1' do
with_form_for @user, :name, error: true
assert_select 'span.error', "can't be blank"
end
test 'input with custom error does not generate the error if there is no error on the attribute' do 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" error_text = "Super User Active! can't be blank"
with_form_for @user, :active, error: error_text with_form_for @user, :active, error: error_text