From 726040adf90e0178f634352fc78d0151cf254791 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 27 Apr 2011 21:58:59 +0200 Subject: [PATCH] Add full_error. --- CHANGELOG.rdoc | 1 + lib/simple_form/components/errors.rb | 6 +++++- lib/simple_form/form_builder.rb | 18 ++++++++++++++++++ test/form_builder_test.rb | 22 ++++++++++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index fbeb71b1..398f900d 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -11,6 +11,7 @@ * Add option to disable all HTML5 extensions (by github.com/wolframarnold) * Add input_field helper to form builder (by github.com/jeroenhouben) * Allow inputs to be discovered on demand by placing them at app/inputs (a la formtastic) + * Add full_error on that shows the error with the attribute name * bug fix * Fix for file attributes automatic detection, to work with virtual attributes diff --git a/lib/simple_form/components/errors.rb b/lib/simple_form/components/errors.rb index 81ed15a3..93ce710f 100644 --- a/lib/simple_form/components/errors.rb +++ b/lib/simple_form/components/errors.rb @@ -12,7 +12,11 @@ module SimpleForm end def error_text - errors.send(error_method) + if options[:error_prefix] + options[:error_prefix] + " " + errors.send(error_method) + else + errors.send(error_method) + end end def error_method diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index 2127e652..a8c6f823 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -204,6 +204,24 @@ module SimpleForm SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, options).error end + # Return the error but also considering its name. This is used + # when errors for a hidden field need to be shown. + # + # == Examples + # + # f.full_error :token #=> Token is invalid + # + def full_error(attribute_name, options={}) + prefix = options.delete(:prefix) || if object.class.respond_to?(:human_attribute_name) + object.class.human_attribute_name(attribute_name.to_s) + else + attribute_name.to_s.humanize + end + + options[:error_prefix] = prefix + error(attribute_name, options) + end + # Creates a hint tag for the given attribute. Accepts a symbol indicating # an attribute for I18n lookup or a string. All the given options are sent # as :hint_html. diff --git a/test/form_builder_test.rb b/test/form_builder_test.rb index 58f9d99f..9372d7fe 100644 --- a/test/form_builder_test.rb +++ b/test/form_builder_test.rb @@ -27,6 +27,12 @@ class FormBuilderTest < ActionView::TestCase end end + def with_full_error_for(object, *args) + with_concat_form_for(object) do |f| + f.full_error(*args) + end + end + def with_hint_for(object, *args) with_concat_form_for(object) do |f| f.hint(*args) @@ -455,6 +461,22 @@ class FormBuilderTest < ActionView::TestCase assert_select 'span.error#name_error', "can't be blank" end + # FULL ERRORS + test 'builder should generate an full error tag for the attribute' do + with_full_error_for @user, :name + assert_select 'span.error', "Super User Name! can't be blank" + end + + test 'builder should generate an full error tag with a clean HTML' do + with_full_error_for @user, :name + assert_no_select 'span.error[error_html]' + end + + test 'builder should allow passing options to full error tag' do + with_full_error_for @user, :name, :id => 'name_error', :prefix => "Your name" + assert_select 'span.error#name_error', "Your name can't be blank" + end + # HINTS test 'builder should generate a hint tag for the attribute' do store_translations(:en, :simple_form => { :hints => { :user => { :name => "Add your name" }}}) do