phoenixframework / phoenix_live_view

Rich, real-time user experiences with server-rendered HTML
https://hex.pm/packages/phoenix_live_view
MIT License
6.23k stars 934 forks source link

`.inputs_for` ignores `:id` option #3488

Closed Serabe closed 3 weeks ago

Serabe commented 3 weeks ago

Environment

Actual behavior

Passing an id option to .inputs_for is ignored. When inputs_for/3 was a thing, using the :id option would let us customise the id generated. Since inputs_for/3 no longer exists and inputs_for/4 generates an error when being used with LiveView, we migrated our code to use .input_for. One of our forms requires customising the ID but doing anything on the sorts of:

~H"""
<.inputs_for :let={subform} field={form[:subfield]} id="some-custom-id">
  <% # Some content here %>
</.inputs_for>
"""

Does not affect the output at all.

Moreover, this bug is demonstrated in the test suite for .inputs_for

    test "with naming options" do
      assigns = %{}

      template = ~H"""
      <.form :let={f} as={:myform}>
        <.inputs_for :let={finner} field={f[:inner]} } id="test" as={:name}>
          <input id={finner[:foo].id} name={finner[:foo].name} type="text" />
        </.inputs_for>
      </.form>
      """

      assert t2h(template) ==
               ~X"""
               <form>
                 <input type="hidden" name="name[_persistent_id]" value="0"> </input>
                 <input id="myform_inner_0_foo" name="name[foo]" type="text"></input>
               </form>
               """
    end

As seen above, thought the option id="test" is being given to .inputs_for, "test" is nowhere to be found in the output.

Expected behavior

In the test above, id should look something like test_inner_0_foo.