Hi! I ran into this head-scratcher...
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 1.5.3 (Hex package) (mix)
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
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?
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 ^^
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:
Let me know if you run into any issues.