allow valid class to inputs if value is present & valid

* allow valid class to inputs if value is present & valid
* template and implementation pattern:
  * https://github.com/plataformatec/simple_form/pull/1552
This commit is contained in:
m5o 2018-03-22 22:21:29 +01:00
parent d008a2a4b1
commit f45e795798
6 changed files with 13 additions and 5 deletions

View File

@ -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)

View File

@ -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:

View File

@ -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 }

View File

@ -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

View File

@ -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

View File

@ -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