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

Fix name_when_new validation on multiselect with form scope name #169

Merged
merged 1 commit into from
May 21, 2024
Merged
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
2 changes: 1 addition & 1 deletion app/presenters/hotwire_combobox/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realized, would be good to use the hidden field name in the error message as well.

Would appreciate a separate PR if you have time. No worries if not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I will make separate PR.

end
Expand Down
11 changes: 11 additions & 0 deletions test/helpers/hotwire_combobox/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down