From 80bc7c96d14190bb2007dafa877741c758bede18 Mon Sep 17 00:00:00 2001 From: Rei Date: Tue, 21 May 2024 13:23:43 +0800 Subject: [PATCH] fix name_when_new on multiselect with form scope name --- app/presenters/hotwire_combobox/component.rb | 2 +- test/helpers/hotwire_combobox/helper_test.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/presenters/hotwire_combobox/component.rb b/app/presenters/hotwire_combobox/component.rb index 07e6e3b..d2be79c 100644 --- a/app/presenters/hotwire_combobox/component.rb +++ b/app/presenters/hotwire_combobox/component.rb @@ -204,7 +204,7 @@ def name_when_new def name_when_new_on_multiselect_must_match_original_name return unless multiselect? && name_when_new.present? - unless name_when_new.to_s == name + unless name_when_new.to_s == hidden_field_name errors.add :name_when_new, :must_match_original_name, message: "must match the regular name ('#{name}', in this case) on multiselect comboboxes." end diff --git a/test/helpers/hotwire_combobox/helper_test.rb b/test/helpers/hotwire_combobox/helper_test.rb index 897e53b..3f2d4c4 100644 --- a/test/helpers/hotwire_combobox/helper_test.rb +++ b/test/helpers/hotwire_combobox/helper_test.rb @@ -116,9 +116,20 @@ class HotwireCombobox::HelperTest < ApplicationViewTestCase combobox_tag :foo, multiselect_chip_src: "some_path", name_when_new: :bar end + assert_raises ActiveModel::ValidationError do + form_with scope: :foo, url: "some_path" do |form| + form.combobox :bar, multiselect_chip_src: "some_path", name_when_new: "foo[baz]" + end + end + assert_nothing_raised do combobox_tag :foo, multiselect_chip_src: "some_path", name_when_new: :foo combobox_tag :foo, multiselect_chip_src: "some_path", free_text: true + + form_with scope: :foo, url: "some_path" do |form| + form.combobox :bar, multiselect_chip_src: "some_path", name_when_new: "foo[bar]" + form.combobox :bar, multiselect_chip_src: "some_path", free_text: true + end end end