Skip to content

Commit

Permalink
Added error_method to tidy up error messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jul 6, 2010
1 parent dca8631 commit a16789d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
3 changes: 3 additions & 0 deletions lib/generators/simple_form/templates/simple_form.rb
Expand Up @@ -11,6 +11,9 @@
# Default tag used on errors.
# config.error_tag = :span

# Method used to tidy up errors.
# config.error_method = :first

# You can wrap all inputs in a pre-defined tag.
# config.wrapper_tag = :div

Expand Down
4 changes: 4 additions & 0 deletions lib/simple_form.rb
Expand Up @@ -17,6 +17,10 @@ module SimpleForm
mattr_accessor :error_tag
@@error_tag = :span

# Method used to tidy up errors.
mattr_accessor :error_method
@@error_method = :first

# Components used by the form builder.
mattr_accessor :components
@@components = [ :label_input, :hint, :error ]
Expand Down
10 changes: 7 additions & 3 deletions lib/simple_form/components/errors.rb
Expand Up @@ -10,7 +10,11 @@ def error_tag
end

def error_text
errors.to_sentence
errors.send(error_method)
end

def error_method
options[:error_method] || SimpleForm.error_method
end

def error_html_options
Expand All @@ -24,11 +28,11 @@ def errors
end

def errors_on_attribute
Array(object.errors[attribute_name])
object.errors[attribute_name]
end

def errors_on_association
reflection ? Array(object.errors[reflection.name]) : []
reflection ? object.errors[reflection.name] : []
end
end
end
Expand Down
17 changes: 13 additions & 4 deletions test/components/error_test.rb
Expand Up @@ -28,9 +28,18 @@ def with_error_for(object, attribute_name, type, options={}, &block)
assert_select 'span.error', "can't be blank"
end

test 'error should generate messages for attribute with several errors' do
with_error_for @user, :age, :numeric
assert_select 'span.error', 'is not a number and must be greater than 18'
test 'error should generate messages for attribute with one error when using first' do
swap SimpleForm, :error_method => :first do
with_error_for @user, :age, :numeric
assert_select 'span.error', 'is not a number'
end
end

test 'error should generate messages for attribute with several errors when using to_sentence' do
swap SimpleForm, :error_method => :to_sentence do
with_error_for @user, :age, :numeric
assert_select 'span.error', 'is not a number and must be greater than 18'
end
end

test 'error should be able to pass html options' do
Expand All @@ -39,7 +48,7 @@ def with_error_for(object, attribute_name, type, options={}, &block)
end

test 'error should find errors on attribute and association' do
with_error_for @user, :company_id, :select, :setup_association => true
with_error_for @user, :company_id, :select, :setup_association => true, :error_method => :to_sentence
assert_select 'span.error', 'must be valid and company must be present'
end
end
17 changes: 10 additions & 7 deletions test/support/models.rb
Expand Up @@ -91,13 +91,16 @@ def self.reflect_on_association(association)
end

def errors
@errors ||= {
:name => "can't be blank",
:description => "must be longer than 15 characters",
:age => ["is not a number", "must be greater than 18"],
:company => "company must be present",
:company_id => "must be valid"
}
@errors ||= begin
hash = Hash.new { |h,k| h[k] = [] }
hash.merge!(
:name => ["can't be blank"],
:description => ["must be longer than 15 characters"],
:age => ["is not a number", "must be greater than 18"],
:company => ["company must be present"],
:company_id => ["must be valid"]
)
end
end
end

Expand Down

0 comments on commit a16789d

Please sign in to comment.