Skip to content

Commit

Permalink
allow custom error when form does not use a model
Browse files Browse the repository at this point in the history
This PR change the has_errors? method to allow
custom errors on forms without a model
  • Loading branch information
victorperez committed May 28, 2018
1 parent 1e35f20 commit 09c928f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/simple_form/components/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def full_error(wrapper_options = nil)
end

def has_errors?
object && object.respond_to?(:errors) && errors.present?
object_with_errors? || object.nil? && has_custom_error?
end

def has_value?
Expand All @@ -34,6 +34,10 @@ def full_error_text
has_custom_error? ? options[:error] : full_errors.send(error_method)
end

def object_with_errors?
object && object.respond_to?(:errors) && errors.present?
end

def error_method
options[:error_method] || SimpleForm.error_method
end
Expand Down
6 changes: 6 additions & 0 deletions test/form_builder/error_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ def with_full_error_for(object, *args)
assert_no_select 'span.error'
end

test 'input with custom error works when form does not use a model' do
with_form_for :user, :active, error: "Super User Active! cannot be blank"

assert_select 'span.error'
end

test 'input with custom error works when using full_error component' do
swap_wrapper :default, custom_wrapper_with_full_error do
error_text = "Super User Name! cannot be blank"
Expand Down

0 comments on commit 09c928f

Please sign in to comment.