Ensure labels, errors and hints are not generated for hidden inputs.
This commit is contained in:
parent
f811359840
commit
aa7e9302db
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue