Skip to content

Commit 35f432c

Browse files
committed
Use id: in errors_on
1 parent 6bfc697 commit 35f432c

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

lib/bootstrap_form/helpers/bootstrap.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ def errors_on(name, options={})
3434
hide_attribute_name = options[:hide_attribute_name] || false
3535
custom_class = options[:custom_class] || false
3636

37-
tag.div(class: custom_class || "invalid-feedback", id: field_id(name, :feedback)) do
37+
tag.div(
38+
class: custom_class || "invalid-feedback",
39+
id: options[:id].present? ? "#{options[:id]}_feedback" : field_id(name, :feedback)
40+
) do
3841
errors = if hide_attribute_name
3942
object.errors[name]
4043
else

lib/bootstrap_form/inputs/base.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ def bootstrap_field(field_name)
2424

2525
def bootstrap_select_group(field_name)
2626
define_method(:"#{field_name}_with_bootstrap") do |name, options={}, html_options={}|
27+
# Specifying the id for a select doesn't work. The Rails helpers need to generate
28+
# what they generate, and that includes the ids for each select option.
2729
options.delete(:id)
2830
html_options = html_options.reverse_merge(control_class: "form-select")
2931
form_group_builder(name, options, html_options) do

test/bootstrap_form_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,19 @@ def warn(message, ...)
15961596
assert_equivalent_html expected, @builder.errors_on(:email, custom_class: "custom-error-class")
15971597
end
15981598

1599+
test "errors_on with specified id:" do
1600+
@user.email = nil
1601+
assert @user.invalid?
1602+
1603+
expected = <<~HTML
1604+
<div class="invalid-feedback" id="custom-id_feedback">
1605+
Email can't be blank, Email is too short (minimum is 5 characters)
1606+
</div>
1607+
HTML
1608+
1609+
assert_equivalent_html expected, @builder.errors_on(:email, id: "custom-id")
1610+
end
1611+
15991612
test "horizontal-style forms" do
16001613
expected = <<~HTML
16011614
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post">

0 commit comments

Comments
 (0)