Add 'valid' to wrapper on :valid?

This commit is contained in:
Adam Eberlin 2015-12-23 06:59:54 -06:00 committed by m5o
parent e294ee2eda
commit f9871346c0
7 changed files with 29 additions and 16 deletions

View File

@ -14,7 +14,7 @@ SimpleForm.setup do |config|
# stack. The options given below are used to wrap the # stack. The options given below are used to wrap the
# whole input. # whole input.
config.wrappers :default, class: :input, config.wrappers :default, class: :input,
hint_class: :field_with_hint, error_class: :field_with_errors do |b| hint_class: :field_with_hint, error_class: :field_with_errors, valid_class: :field_without_errors do |b|
## Extensions enabled by default ## Extensions enabled by default
# Any of these extensions can be disabled for a # Any of these extensions can be disabled for a
# given input by passing: `f.input EXTENSION_NAME => false`. # given input by passing: `f.input EXTENSION_NAME => false`.

View File

@ -12,7 +12,7 @@ SimpleForm.setup do |config|
config.button_class = 'btn btn-default' config.button_class = 'btn btn-default'
config.boolean_label_class = nil config.boolean_label_class = nil
config.wrappers :vertical_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| config.wrappers :vertical_form, tag: 'div', class: 'form-group', error_class: 'has-error', valid_class: 'no-error' do |b|
b.use :html5 b.use :html5
b.use :placeholder b.use :placeholder
b.optional :maxlength b.optional :maxlength
@ -27,7 +27,7 @@ SimpleForm.setup do |config|
b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } b.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end end
config.wrappers :vertical_file_input, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| config.wrappers :vertical_file_input, tag: 'div', class: 'form-group', error_class: 'has-error', valid_class: 'no-error' do |b|
b.use :html5 b.use :html5
b.use :placeholder b.use :placeholder
b.optional :maxlength b.optional :maxlength
@ -40,7 +40,7 @@ SimpleForm.setup do |config|
b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } b.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end end
config.wrappers :vertical_boolean, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| config.wrappers :vertical_boolean, tag: 'div', class: 'form-group', error_class: 'has-error', valid_class: 'no-error' do |b|
b.use :html5 b.use :html5
b.optional :readonly b.optional :readonly
@ -52,7 +52,7 @@ SimpleForm.setup do |config|
b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } b.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end end
config.wrappers :vertical_radio_and_checkboxes, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| config.wrappers :vertical_radio_and_checkboxes, tag: 'div', class: 'form-group', error_class: 'has-error', valid_class: 'no-error' do |b|
b.use :html5 b.use :html5
b.optional :readonly b.optional :readonly
b.use :label, class: 'control-label' b.use :label, class: 'control-label'
@ -61,7 +61,7 @@ SimpleForm.setup do |config|
b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } b.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end end
config.wrappers :horizontal_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| config.wrappers :horizontal_form, tag: 'div', class: 'form-group', error_class: 'has-error', valid_class: 'no-error' do |b|
b.use :html5 b.use :html5
b.use :placeholder b.use :placeholder
b.optional :maxlength b.optional :maxlength
@ -78,7 +78,7 @@ SimpleForm.setup do |config|
end end
end end
config.wrappers :horizontal_file_input, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| config.wrappers :horizontal_file_input, tag: 'div', class: 'form-group', error_class: 'has-error', valid_class: 'no-error' do |b|
b.use :html5 b.use :html5
b.use :placeholder b.use :placeholder
b.optional :maxlength b.optional :maxlength
@ -93,7 +93,7 @@ SimpleForm.setup do |config|
end end
end end
config.wrappers :horizontal_boolean, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| config.wrappers :horizontal_boolean, tag: 'div', class: 'form-group', error_class: 'has-error', valid_class: 'no-error' do |b|
b.use :html5 b.use :html5
b.optional :readonly b.optional :readonly
@ -107,7 +107,7 @@ SimpleForm.setup do |config|
end end
end end
config.wrappers :horizontal_radio_and_checkboxes, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| config.wrappers :horizontal_radio_and_checkboxes, tag: 'div', class: 'form-group', error_class: 'has-error', valid_class: 'no-error' do |b|
b.use :html5 b.use :html5
b.optional :readonly b.optional :readonly
@ -120,7 +120,7 @@ SimpleForm.setup do |config|
end end
end end
config.wrappers :inline_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| config.wrappers :inline_form, tag: 'div', class: 'form-group', error_class: 'has-error', valid_class: 'no-error' do |b|
b.use :html5 b.use :html5
b.use :placeholder b.use :placeholder
b.optional :maxlength b.optional :maxlength
@ -135,7 +135,7 @@ SimpleForm.setup do |config|
b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } b.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end end
config.wrappers :multi_select, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| config.wrappers :multi_select, tag: 'div', class: 'form-group', error_class: 'has-error', valid_class: 'no-error' do |b|
b.use :html5 b.use :html5
b.optional :readonly b.optional :readonly
b.use :label, class: 'control-label' b.use :label, class: 'control-label'

View File

@ -15,7 +15,7 @@ SimpleForm.setup do |config|
# doesn't provide styles for hints. You will need to provide your own CSS styles for hints. # doesn't provide styles for hints. You will need to provide your own CSS styles for hints.
# Uncomment them to enable hints. # Uncomment them to enable hints.
config.wrappers :vertical_form, class: :input, hint_class: :field_with_hint, error_class: :error do |b| config.wrappers :vertical_form, class: :input, hint_class: :field_with_hint, error_class: :error, valid_class: :valid do |b|
b.use :html5 b.use :html5
b.use :placeholder b.use :placeholder
b.optional :maxlength b.optional :maxlength
@ -29,7 +29,7 @@ SimpleForm.setup do |config|
# b.use :hint, wrap_with: { tag: :span, class: :hint } # b.use :hint, wrap_with: { tag: :span, class: :hint }
end end
config.wrappers :horizontal_form, tag: 'div', class: 'row', hint_class: :field_with_hint, error_class: :error do |b| config.wrappers :horizontal_form, tag: 'div', class: 'row', hint_class: :field_with_hint, error_class: :error, valid_class: :valid do |b|
b.use :html5 b.use :html5
b.use :placeholder b.use :placeholder
b.optional :maxlength b.optional :maxlength
@ -71,7 +71,7 @@ SimpleForm.setup do |config|
# Note that you need to adapt this wrapper to your needs. If you need a 4 # Note that you need to adapt this wrapper to your needs. If you need a 4
# columns form then change the wrapper class to 'small-3', if you need # columns form then change the wrapper class to 'small-3', if you need
# only two use 'small-6' and so on. # only two use 'small-6' and so on.
config.wrappers :inline_form, tag: 'div', class: 'column small-4', hint_class: :field_with_hint, error_class: :error do |b| config.wrappers :inline_form, tag: 'div', class: 'column small-4', hint_class: :field_with_hint, error_class: :error, valid_class: :valid do |b|
b.use :html5 b.use :html5
b.use :placeholder b.use :placeholder
b.optional :maxlength b.optional :maxlength
@ -90,7 +90,7 @@ SimpleForm.setup do |config|
# Examples of use: # Examples of use:
# - wrapper_html: {class: 'row'}, custom_wrapper_html: {class: 'column small-12'} # - wrapper_html: {class: 'row'}, custom_wrapper_html: {class: 'column small-12'}
# - custom_wrapper_html: {class: 'column small-3 end'} # - custom_wrapper_html: {class: 'column small-3 end'}
config.wrappers :customizable_wrapper, tag: 'div', error_class: :error do |b| config.wrappers :customizable_wrapper, tag: 'div', error_class: :error, valid_class: :valid do |b|
b.use :html5 b.use :html5
b.optional :readonly b.optional :readonly

View File

@ -229,7 +229,7 @@ See https://github.com/plataformatec/simple_form/pull/997 for more information.
SimpleForm::Wrappers::Root.new(builder.to_a, options) SimpleForm::Wrappers::Root.new(builder.to_a, options)
end end
wrappers class: :input, hint_class: :field_with_hint, error_class: :field_with_errors do |b| wrappers class: :input, hint_class: :field_with_hint, error_class: :field_with_errors, valid_class: :field_without_errors do |b|
b.use :html5 b.use :html5
b.use :min_max b.use :min_max

View File

@ -14,6 +14,16 @@ module SimpleForm
object && object.respond_to?(:errors) && errors.present? object && object.respond_to?(:errors) && errors.present?
end end
def has_value?
return unless object && object.respond_to?(attribute_name)
object.send(attribute_name).present?
end
def valid?
!has_errors? && has_value?
end
protected protected
def error_text def error_text

View File

@ -30,6 +30,7 @@ module SimpleForm
end end
css << (options[:wrapper_error_class] || @defaults[:error_class]) if input.has_errors? css << (options[:wrapper_error_class] || @defaults[:error_class]) if input.has_errors?
css << (options[:wrapper_hint_class] || @defaults[:hint_class]) if input.has_hint? css << (options[:wrapper_hint_class] || @defaults[:hint_class]) if input.has_hint?
css << (options[:wrapper_valid_class] || @defaults[:valid_class]) if input.valid?
css.compact css.compact
end end
end end

View File

@ -242,6 +242,7 @@ class FormBuilderTest < ActionView::TestCase
test 'builder generates file for file columns' do test 'builder generates file for file columns' do
@user.avatar = MiniTest::Mock.new @user.avatar = MiniTest::Mock.new
@user.avatar.expect(:public_filename, true) @user.avatar.expect(:public_filename, true)
@user.avatar.expect(:!, false)
with_form_for @user, :avatar with_form_for @user, :avatar
assert_select 'form input#user_avatar.file' assert_select 'form input#user_avatar.file'
@ -250,6 +251,7 @@ class FormBuilderTest < ActionView::TestCase
test 'builder generates file for attributes that are real db columns but have file methods' do test 'builder generates file for attributes that are real db columns but have file methods' do
@user.home_picture = MiniTest::Mock.new @user.home_picture = MiniTest::Mock.new
@user.home_picture.expect(:mounted_as, true) @user.home_picture.expect(:mounted_as, true)
@user.home_picture.expect(:!, false)
with_form_for @user, :home_picture with_form_for @user, :home_picture
assert_select 'form input#user_home_picture.file' assert_select 'form input#user_home_picture.file'