Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make error_class work on inner wrapper #1729

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/simple_form/wrappers/many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ def html_options(options)
end

def html_classes(input, options)
@defaults[:class].dup
css = @defaults[:class].dup

if @defaults[:error_class] && input.has_errors?
css << @defaults[:error_class]
end

css.compact
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions test/form_builder/wrapper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,18 @@ class WrapperTest < ActionView::TestCase
assert_no_select 'p.omg_hint'
end
end

test 'inline wrapper has an error class' do
swap_wrapper :default, custom_wrapper_with_error_class do
with_form_for @user, :name
assert_select 'section.custom_wrapper div.custom_error'
end
end

test 'inline wrapper has no error class when there is no error' do
swap_wrapper :default, custom_wrapper_with_error_class do
with_form_for @user, :email
assert_no_select 'section.custom_wrapper div.custom_error'
end
end
end
8 changes: 8 additions & 0 deletions test/support/misc_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ def custom_wrapper_with_unless_blank
end
end

def custom_wrapper_with_error_class
SimpleForm.build tag: :section, class: "custom_wrapper" do |b|
b.wrapper tag: :div, error_class: 'custom_error' do |ba|
ba.optional :hint, wrap_with: { tag: :p, class: 'omg_hint' }
end
end
end

def custom_wrapper_with_input_class
SimpleForm.build tag: :div, class: "custom_wrapper" do |b|
b.use :label
Expand Down