Skip to content

Commit

Permalink
allow valid class to inputs if value is present & valid
Browse files Browse the repository at this point in the history
* allow valid class to inputs if value is present & valid
* template and implementation pattern:
  * heartcombo#1552
  • Loading branch information
m5o committed Mar 23, 2018
1 parent d008a2a commit f45e795
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
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 }
Expand Down
5 changes: 5 additions & 0 deletions lib/simple_form/inputs/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,16 @@ def merge_wrapper_options(options, wrapper_options)
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

Expand Down
2 changes: 2 additions & 0 deletions test/form_builder/wrapper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/support/misc_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def custom_wrapper_with_input_error_class
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

Expand Down

0 comments on commit f45e795

Please sign in to comment.