add fieldWtihErrors class on wrapper with errors (fixes Issue #3)

This commit is contained in:
jduff 2010-05-25 02:52:54 +08:00 committed by José Valim
parent b4f34f01c2
commit 7ce457ca51
5 changed files with 44 additions and 2 deletions

View File

@ -16,6 +16,9 @@ SimpleForm.setup do |config|
# You can wrap all inputs in a pre-defined tag.
# config.wrapper_tag = :div
# CSS class to add to the wrapper if the field has errors
# config.wrapper_errors_class = :fieldWithErrors
# How the label text should be generated altogether with the required text.
# config.label_text = lambda { |label, required| "#{required} #{label}" }

View File

@ -33,6 +33,9 @@ module SimpleForm
mattr_accessor :wrapper_tag
@@wrapper_tag = :div
mattr_accessor :wrapper_errors_class
@@wrapper_errors_class = :fieldWithErrors
# How the label text should be generated altogether with the required text.
mattr_accessor :label_text
@@label_text = lambda { |label, required| "#{required} #{label}" }

View File

@ -13,9 +13,15 @@ module SimpleForm
options[:wrapper_tag] || SimpleForm.wrapper_tag
end
def errors_class
options[:wrapper_errors_class] || SimpleForm.wrapper_errors_class
end
def wrapper_html_options
html_options_for(:wrapper, "input", input_type, required_class)
css_classes = ["input", input_type, required_class]
css_classes << errors_class if object && errors.present?
html_options_for(:wrapper, css_classes)
end
end
end
end
end

View File

@ -0,0 +1,25 @@
require 'test_helper'
class WrapperTest < ActionView::TestCase
def with_error_for(object, attribute_name, options={}, &block)
concat(simple_form_for object do |f|
f.options = options
f.input attribute_name
end)
end
test 'wrapper should not have error class for attribute without errors' do
with_error_for @user, :active
assert_no_select 'div.fieldWithErrors'
end
test 'wrapper should not have error class when object is not present' do
with_error_for :project, :name
assert_no_select 'div.fieldWithErrors'
end
test 'wrapper should add error class for attribute with errors' do
with_error_for @user, :name
assert_select 'div.fieldWithErrors'
end
end

View File

@ -288,6 +288,11 @@ class FormBuilderTest < ActionView::TestCase
assert_select 'form b.required.string'
end
test 'builder wrapping tag adds erros class for attribute with errors' do
with_form_for @user, :name
assert_select 'form div.input.required.string.fieldWithErrors'
end
# WITHOUT OBJECT
test 'builder should generate properly when object is not present' do
with_form_for :project, :name