Phoenix_live_view: Test helper form/3 doesnt allow hidden input values to be set.

Created on 19 Jun 2020  路  4Comments  路  Source: phoenixframework/phoenix_live_view

Hi! I ran into this head-scratcher...

Environment

  • Elixir version (elixir -v):

Erlang/OTP 22 [erts-10.7.2] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1]

Elixir 1.10.3 (compiled with Erlang/OTP 22)

  • Phoenix version (mix deps):

phoenix 1.5.3 (Hex package) (mix)

  • Phoenix LiveView version (mix deps): phoenix_live_view 0.13.3 (Hex package) (mix)
  • NodeJS version (node -v): v12.15.0
  • NPM version (npm -v): 6.13.4
  • Operating system: Ubuntu
  • Browsers you attempted to reproduce this bug on (the more the merrier): n/a
  • Does the problem persist after removing "assets/node_modules" and trying again? Yes/no: Yes.

Actual behavior

Seeing this error in my liveview tests

     ** (ArgumentError) value for hidden "submittedButton" must be one of [""], got: "submit-and-next"             
     code: |> render_submit()                   
     stacktrace:                                                                                                   
       (phoenix_live_view 0.13.3) lib/phoenix_live_view/test/live_view_test.ex:873: Phoenix.LiveViewTest.call/2     
       test/brmbl_web/live/owner/process_chain_live_test.exs:133: (test)                                                                         

for this bit of test code:

      assert {:ok, _, html} =
               index_live
               |> form("#chain-form",
                 process_chain: %{name: name, description: descr},
                 submittedButton: "submit-and-next"
               )
               |> render_submit()
               |> follow_redirect(
                 conn,
                 Routes.owner_process_chain_index_path(conn, :new)
               )

and this html:

  <%= f = form_for @changeset, "#",
id: "chain-form",
phx_target: @myself,
phx_change: "validate",
phx_submit: "save" %>

[[[  ... form fields removed for brevity ]]]

      <input type="hidden" name="submittedButton" value="" />
      <%= submit @button_label, name: "submit-only", phx_hook: "SetButtonState", phx_disable_with: "Adding...", class: "btn btn-primary lift"%>
      <%= if @save_and_add_button_label do %>
        <%= submit @save_and_add_button_label, name: "submit-and-next", phx_hook: "SetButtonState", phx_disable_with: "Adding...", class: "btn btn-primary lift"%>
      <% end %>
</div>

and this event handler:

  def handle_event(
        "save",
        %{"process_chain" => chain_params, "submittedButton" => button},
        socket
      ) do
    save_process_chain(socket, socket.assigns.action, chain_params, button)
  end

Expected behavior

form/3 should allow the hidden input's value to be set.

For more context: I'm the hidden input field to determine which form button was pressed per https://github.com/phoenixframework/phoenix_live_view/issues/511#issuecomment-563875328

The feature in question works perfectly in the browser - so either a problem with LiveViewTest form/3's handling of hidden form inputs, or I'm using it incorrectly?

Most helpful comment

Yes, this is intentional, since form is meant to mimic what the user can actually do. You can pass hidden values directly as the third value of render_submit/render_change:

view |> form(..., ...) |> render_submit(%{"hidden_value" => "example"})

Let me know if you run into any issues.

All 4 comments

Yes, this is intentional, since form is meant to mimic what the user can actually do. You can pass hidden values directly as the third value of render_submit/render_change:

view |> form(..., ...) |> render_submit(%{"hidden_value" => "example"})

Let me know if you run into any issues.

Whoa - thanks for the quick reply!

That indeed fixed it.

Root cause: something between my keyboard and my chair.

Root cause: something between my keyboard and my chair.

I disagree. :) It is definitely a corner case and we don't have docs for it. Would you like to send a PR that improves the docs for form to help the next human that may run into this scenario? thanks!

@josevalim it's my pleasure. PR above ^^

Was this page helpful?
0 / 5 - 0 ratings