From aa7e9302db5eddbfbc4112f53e868c5c01b8287e Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Wed, 9 Dec 2009 09:38:51 -0200 Subject: [PATCH] Ensure labels, errors and hints are not generated for hidden inputs. --- lib/simple_form/error.rb | 12 ++++++++++-- lib/simple_form/form_builder.rb | 4 ++++ lib/simple_form/hint.rb | 6 +++++- lib/simple_form/label.rb | 6 +++++- test/error_test.rb | 7 +++++++ test/hint_test.rb | 7 +++++++ test/label_test.rb | 7 +++++++ 7 files changed, 45 insertions(+), 4 deletions(-) diff --git a/lib/simple_form/error.rb b/lib/simple_form/error.rb index a1b6b19a..0ede06a1 100644 --- a/lib/simple_form/error.rb +++ b/lib/simple_form/error.rb @@ -4,9 +4,17 @@ module SimpleForm private def generate_error - return '' unless errors = @object.errors[@attribute] - errors = errors.is_a?(Array) ? errors.to_sentence : errors + return '' if skip_error? || (errors = find_errors).blank? + errors = errors.to_sentence if errors.respond_to?(:to_sentence) @template.content_tag(:span, errors, :class => 'error') end + + def skip_error? + hidden_input? + end + + def find_errors + @object.errors[@attribute] + end end end diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index 7f15e450..ea78324e 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -46,6 +46,10 @@ module SimpleForm end end + def hidden_input? + @input_type == :hidden + end + def translate_form(scope, default='') lookups = [:"#{@object_name}.#{@attribute}", :"#{@attribute}", default] translate(lookups.shift, :scope => :"simple_form.#{scope}", :default => lookups) diff --git a/lib/simple_form/hint.rb b/lib/simple_form/hint.rb index 15b1dfd1..1ee1f80d 100644 --- a/lib/simple_form/hint.rb +++ b/lib/simple_form/hint.rb @@ -4,10 +4,14 @@ module SimpleForm private def generate_hint - return '' if @options[:hint] == false || (text = hint_text).blank? + return '' if skip_hint? || (text = hint_text).blank? @template.content_tag(:span, text, :class => 'hint') end + def skip_hint? + @options[:hint] == false || hidden_input? + end + def hint_text @options[:hint] || translate_hint end diff --git a/lib/simple_form/label.rb b/lib/simple_form/label.rb index 2c70db9b..90b0766c 100644 --- a/lib/simple_form/label.rb +++ b/lib/simple_form/label.rb @@ -4,12 +4,16 @@ module SimpleForm private def generate_label - return '' if @options[:label] == false + return '' if skip_label? html_options = { :class => "#{@input_type} #{required_class}".strip } html_options[:for] = @options[:html][:id] if @options.key?(:html) label(@attribute, label_text, html_options) end + def skip_label? + @options[:label] == false || hidden_input? + end + def label_text required_text << (@options[:label] || translate_label) end diff --git a/test/error_test.rb b/test/error_test.rb index de18ba8b..6d9b472b 100644 --- a/test/error_test.rb +++ b/test/error_test.rb @@ -30,4 +30,11 @@ class ErrorTest < ActionView::TestCase assert_select 'form span.error', "is not a number and must be greater than 18" assert_select 'form span.error', "must be present and must be greater than 0" end + + test 'errors should not be generated for hidden fields' do + simple_form_for @super_user do |f| + concat f.input :name, :as => :hidden + end + assert_no_select 'form span.error' + end end diff --git a/test/hint_test.rb b/test/hint_test.rb index a588e692..834b58aa 100644 --- a/test/hint_test.rb +++ b/test/hint_test.rb @@ -44,4 +44,11 @@ class ErrorTest < ActionView::TestCase assert_no_select 'form span.hint' end end + + test 'hint should not be generated for hidden fields' do + simple_form_for @user do |f| + concat f.input :name, :hint => 'Bla bla bla', :as => :hidden + end + assert_no_select 'form span.hint' + end end diff --git a/test/label_test.rb b/test/label_test.rb index 43c10193..86dbb75f 100644 --- a/test/label_test.rb +++ b/test/label_test.rb @@ -145,4 +145,11 @@ class LabelTest < ActionView::TestCase assert_select 'form input[id=user_name]' assert_select 'form label[for=user_name]' end + + test 'label should not be generated for hidden fields' do + simple_form_for @user do |f| + concat f.input :name, :as => :hidden + end + assert_no_select 'label' + end end