diff --git a/spec/lucky/ext/select_helpers_spec.cr b/spec/lucky/ext/select_helpers_spec.cr index e1c976bf1..f127a6acf 100644 --- a/spec/lucky/ext/select_helpers_spec.cr +++ b/spec/lucky/ext/select_helpers_spec.cr @@ -79,21 +79,21 @@ end describe Lucky::SelectHelpers do it "renders select" do view.render_select(form.company_id).html.to_s.should eq <<-HTML - + HTML view.render_disabled_select(form.company_id).html.to_s.should eq <<-HTML - + HTML end it "renders multi-select" do view.render_multi_select(form.tags).html.to_s.should eq <<-HTML - + HTML view.render_disabled_multi_select(form.tags).html.to_s.should eq <<-HTML - + HTML end diff --git a/src/lucky/ext/select_helpers.cr b/src/lucky/ext/select_helpers.cr index a8ed8c70c..4b94a38a0 100644 --- a/src/lucky/ext/select_helpers.cr +++ b/src/lucky/ext/select_helpers.cr @@ -1,13 +1,13 @@ module Lucky::SelectHelpers def select_input(field : Avram::PermittedAttribute, attrs : Array(Symbol) = [] of Symbol, **html_options, &) : Nil - select_tag attrs, merge_options(html_options, {"name" => input_name(field)}) do + select_tag attrs, merge_options(html_options, {"id" => input_id(field), "name" => input_name(field)}) do yield end end def multi_select_input(field : Avram::PermittedAttribute(Array), attrs : Array(Symbol) = [] of Symbol, **html_options, &) : Nil merged_attrs = [:multiple].concat(attrs) - select_tag merged_attrs, merge_options(html_options, {"name" => input_name(field)}) do + select_tag merged_attrs, merge_options(html_options, {"id" => input_id(field), "name" => input_name(field)}) do yield end end @@ -44,6 +44,14 @@ module Lucky::SelectHelpers option(label, value: "") end + private def input_id(field) + "#{field.param_key}_#{field.name}" + end + + private def input_id(field : Avram::PermittedAttribute(Array)) + "#{field.param_key}_#{field.name}_#{array_id_counter[field.name]}" + end + private def input_name(field) "#{field.param_key}:#{field.name}" end