Skip to content

Commit

Permalink
Allow forms for objects that don't respond to the "errors" method.
Browse files Browse the repository at this point in the history
Useful for non-model forms, such as search forms.
  • Loading branch information
Javier Martín committed Aug 7, 2010
1 parent ff77bee commit db9c1b8
Show file tree
Hide file tree
Showing 2 changed files with 12 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 @@ -32,7 +32,11 @@ def errors
end

def errors_on_attribute
object.errors[attribute_name]
if object.respond_to?(:errors)
object.errors[attribute_name]
else
[]
end
end

def errors_on_association
Expand Down
7 changes: 7 additions & 0 deletions test/components/error_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ def with_error_for(object, attribute_name, type, options={}, &block)
assert_no_select 'span.error'
end

test "error should not generate messages when object doesn't respond to errors method" do
@user.instance_eval {undef errors}
with_error_for @user, :name, :string
assert_no_select 'span.error'
end


test 'error should generate messages for attribute with single error' do
with_error_for @user, :name, :string
assert_select 'span.error', "can't be blank"
Expand Down

0 comments on commit db9c1b8

Please sign in to comment.