Show custom error only if we have the error on the attribute
This commit is contained in:
parent
ea679105dc
commit
23294b5d67
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue