diff --git a/lib/bootstrap_form/components/validation.rb b/lib/bootstrap_form/components/validation.rb index b054482f3..8fce30419 100644 --- a/lib/bootstrap_form/components/validation.rb +++ b/lib/bootstrap_form/components/validation.rb @@ -65,16 +65,20 @@ def generate_error(name) content_tag(help_tag, help_text, class: help_klass) end + # rubocop:disable Metrics/AbcSize def get_error_messages(name) - messages = object.errors[name] object.class.try(:reflections)&.each do |association_name, a| next unless a.is_a?(ActiveRecord::Reflection::BelongsToReflection) next unless a.foreign_key == name.to_s - messages << object.errors[association_name] + object.errors[association_name].each do |error| + object.errors.add(name, error) + end end - messages.join(", ") + + object.errors[name].join(", ") end + # rubocop:enable Metrics/AbcSize end end end diff --git a/test/bootstrap_checkbox_test.rb b/test/bootstrap_checkbox_test.rb index 01eb49c2e..e6e279b3a 100644 --- a/test/bootstrap_checkbox_test.rb +++ b/test/bootstrap_checkbox_test.rb @@ -1,4 +1,4 @@ -require_relative "./test_helper" +require_relative "test_helper" class BootstrapCheckboxTest < ActionView::TestCase include BootstrapForm::ActionViewExtensions::FormHelper diff --git a/test/bootstrap_configuration_test.rb b/test/bootstrap_configuration_test.rb index 44c150931..ba1a37f74 100644 --- a/test/bootstrap_configuration_test.rb +++ b/test/bootstrap_configuration_test.rb @@ -1,4 +1,4 @@ -require_relative "./test_helper" +require_relative "test_helper" class BootstrapConfigurationTest < ActionView::TestCase test "has default form attributes" do diff --git a/test/bootstrap_fields_test.rb b/test/bootstrap_fields_test.rb index b1a50fe0b..7838f4d42 100644 --- a/test/bootstrap_fields_test.rb +++ b/test/bootstrap_fields_test.rb @@ -1,4 +1,4 @@ -require_relative "./test_helper" +require_relative "test_helper" class BootstrapFieldsTest < ActionView::TestCase include BootstrapForm::ActionViewExtensions::FormHelper @@ -100,6 +100,22 @@ class BootstrapFieldsTest < ActionView::TestCase assert_equivalent_html expected, bootstrap_form_for(@user) { |f| f.file_field(:misc) } end + test "errors are correctly displayed for belongs_to assoication fields" do + @address.valid? + + expected = <<~HTML +
+ HTML + assert_equivalent_html expected, bootstrap_form_for(@address, url: users_path) { |f| f.text_field(:user_id) } + end + test "hidden fields are supported" do expected = <<~HTML diff --git a/test/bootstrap_form_group_test.rb b/test/bootstrap_form_group_test.rb index d19496d26..d457c9b2b 100644 --- a/test/bootstrap_form_group_test.rb +++ b/test/bootstrap_form_group_test.rb @@ -1,4 +1,4 @@ -require_relative "./test_helper" +require_relative "test_helper" class BootstrapFormGroupTest < ActionView::TestCase include BootstrapForm::ActionViewExtensions::FormHelper diff --git a/test/bootstrap_form_test.rb b/test/bootstrap_form_test.rb index 7f849babb..481c363d8 100644 --- a/test/bootstrap_form_test.rb +++ b/test/bootstrap_form_test.rb @@ -1,4 +1,4 @@ -require_relative "./test_helper" +require_relative "test_helper" class BootstrapFormTest < ActionView::TestCase include BootstrapForm::ActionViewExtensions::FormHelper diff --git a/test/bootstrap_other_components_test.rb b/test/bootstrap_other_components_test.rb index cc0afb39c..3f90955d2 100644 --- a/test/bootstrap_other_components_test.rb +++ b/test/bootstrap_other_components_test.rb @@ -1,4 +1,4 @@ -require_relative "./test_helper" +require_relative "test_helper" class BootstrapOtherComponentsTest < ActionView::TestCase include BootstrapForm::ActionViewExtensions::FormHelper diff --git a/test/bootstrap_radio_button_test.rb b/test/bootstrap_radio_button_test.rb index efa831b17..75c30a415 100644 --- a/test/bootstrap_radio_button_test.rb +++ b/test/bootstrap_radio_button_test.rb @@ -1,4 +1,4 @@ -require_relative "./test_helper" +require_relative "test_helper" class BootstrapRadioButtonTest < ActionView::TestCase include BootstrapForm::ActionViewExtensions::FormHelper diff --git a/test/bootstrap_rich_text_area_test.rb b/test/bootstrap_rich_text_area_test.rb index 1e4f8adb0..f95d3dad8 100644 --- a/test/bootstrap_rich_text_area_test.rb +++ b/test/bootstrap_rich_text_area_test.rb @@ -1,4 +1,4 @@ -require_relative "./test_helper" +require_relative "test_helper" require "minitest/mock" if Rails::VERSION::STRING > "6" diff --git a/test/bootstrap_selects_test.rb b/test/bootstrap_selects_test.rb index b35a9d708..19dba2ff1 100644 --- a/test/bootstrap_selects_test.rb +++ b/test/bootstrap_selects_test.rb @@ -1,4 +1,4 @@ -require_relative "./test_helper" +require_relative "test_helper" class BootstrapSelectsTest < ActionView::TestCase include BootstrapForm::ActionViewExtensions::FormHelper diff --git a/test/bootstrap_without_fields_test.rb b/test/bootstrap_without_fields_test.rb index 57e123c6e..fcb555e4e 100644 --- a/test/bootstrap_without_fields_test.rb +++ b/test/bootstrap_without_fields_test.rb @@ -1,4 +1,4 @@ -require_relative "./test_helper" +require_relative "test_helper" class BootstrapWithoutFieldsTest < ActionView::TestCase include BootstrapForm::ActionViewExtensions::FormHelper diff --git a/test/special_form_class_models_test.rb b/test/special_form_class_models_test.rb index 7e661b7ed..98874145a 100644 --- a/test/special_form_class_models_test.rb +++ b/test/special_form_class_models_test.rb @@ -1,4 +1,4 @@ -require_relative "./test_helper" +require_relative "test_helper" class SpecialFormClassModelsTest < ActionView::TestCase include BootstrapForm::ActionViewExtensions::FormHelper diff --git a/test/test_helper.rb b/test/test_helper.rb index fef751d67..706278901 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -12,6 +12,7 @@ class ActionView::TestCase def setup_test_fixture + @address = Address.new(street: "Foo") @user = User.new(email: "steve@example.com", password: "secret", comments: "my comment") @builder = BootstrapForm::FormBuilder.new(:user, @user, self, {}) @horizontal_builder = BootstrapForm::FormBuilder.new(:user, @user, self,