diff --git a/CHANGELOG.md b/CHANGELOG.md index 9789408a..aa68e1f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Remove support from Rails 4.0, 4.1 and 4.2. [@feliperenan](https://github.com/feliperenan) * Add support for citext, hstore, json & jsonb column types. [@swrobel](https://github.com/swrobel) * Add :valid_class on input wrapper when value is present and valid [@aeberlin](https://github.com/aeberlin), [@m5o](https://github.com/m5o) +* Allow :valid_class to inputs when value is present and valid. [@m5o](https://github.com/m5o) ### Bug fix * Fix horizontal form label position, from right to text-right. [@cavpollo](https://github.com/cavpollo) diff --git a/README.md b/README.md index 55768799..3653c9f8 100644 --- a/README.md +++ b/README.md @@ -906,12 +906,12 @@ You can customize _Form components_ passing options to them: ```ruby config.wrappers do |b| - b.use :label_input, class: 'label-input-class', error_class: 'is-invalid' + b.use :label_input, class: 'label-input-class', error_class: 'is-invalid', valid_class: 'is-valid' end ``` -This you set the input and label class to `'label-input-class'` and will set the class `'is-invalid'` -when the input has errors. +This you set the input and label class to `'label-input-class'` and will set the class `'is-invalid'` +when the input has errors and `'is-valid'` if the input is valid. If you want to customize the custom _Form components_ on demand you can give it a name like this: diff --git a/lib/generators/simple_form/templates/config/initializers/simple_form.rb b/lib/generators/simple_form/templates/config/initializers/simple_form.rb index 2cb8881d..01df9258 100644 --- a/lib/generators/simple_form/templates/config/initializers/simple_form.rb +++ b/lib/generators/simple_form/templates/config/initializers/simple_form.rb @@ -52,7 +52,7 @@ SimpleForm.setup do |config| b.optional :readonly ## Inputs - # b.use :input, class: 'input', error_class: 'is-invalid' + # b.use :input, class: 'input', error_class: 'is-invalid', valid_class: 'is-valid' b.use :label_input b.use :hint, wrap_with: { tag: :span, class: :hint } b.use :error, wrap_with: { tag: :span, class: :error } diff --git a/lib/simple_form/inputs/base.rb b/lib/simple_form/inputs/base.rb index 54f8ef07..bde6c1ea 100644 --- a/lib/simple_form/inputs/base.rb +++ b/lib/simple_form/inputs/base.rb @@ -211,11 +211,16 @@ module SimpleForm def set_input_classes(wrapper_options) wrapper_options = wrapper_options.dup error_class = wrapper_options.delete(:error_class) + valid_class = wrapper_options.delete(:valid_class) if error_class.present? && has_errors? wrapper_options[:class] = "#{wrapper_options[:class]} #{error_class}" end + if valid_class.present? && valid? + wrapper_options[:class] = "#{wrapper_options[:class]} #{valid_class}" + end + wrapper_options end diff --git a/test/form_builder/wrapper_test.rb b/test/form_builder/wrapper_test.rb index e66927da..fd2dc8c4 100644 --- a/test/form_builder/wrapper_test.rb +++ b/test/form_builder/wrapper_test.rb @@ -54,7 +54,9 @@ class WrapperTest < ActionView::TestCase @user.instance_eval { undef errors } with_form_for @user, :name, wrapper: custom_wrapper_with_input_valid_class assert_select 'div.field_without_errors' + assert_select 'input.is-valid' assert_no_select 'div.field_with_errors' + assert_no_select 'input.is-invalid' end test 'wrapper adds hint class for attribute with a hint' do diff --git a/test/support/misc_helpers.rb b/test/support/misc_helpers.rb index 6b3ccc56..5012bb6b 100644 --- a/test/support/misc_helpers.rb +++ b/test/support/misc_helpers.rb @@ -216,7 +216,7 @@ module MiscHelpers def custom_wrapper_with_input_valid_class SimpleForm.build tag: :div, class: "custom_wrapper", valid_class: :field_without_errors do |b| b.use :label - b.use :input, class: 'inline-class' + b.use :input, class: 'inline-class', valid_class: 'is-valid' end end