Skip to content

Commit

Permalink
Downcase form method in <.form> (#3431)
Browse files Browse the repository at this point in the history
The HTML form method attribute is case insensitive, so if we get a
get / post we always use the downcased version.

Fixes #3422.
  • Loading branch information
SteffenDE committed Sep 21, 2024
1 parent 3aec33f commit e5c44d8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/phoenix_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2362,8 +2362,13 @@ defmodule Phoenix.Component do
end

defp form_method(nil), do: {"post", nil}
defp form_method(method) when method in ~w(get post), do: {method, nil}
defp form_method(method) when is_binary(method), do: {"post", method}

defp form_method(method) when is_binary(method) do
case String.downcase(method) do
method when method in ~w(get post) -> {method, nil}
_ -> {"post", method}
end
end

@doc """
Renders nested form inputs for associations or embeds.
Expand Down
33 changes: 33 additions & 0 deletions test/phoenix_component/components_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,39 @@ defmodule Phoenix.LiveView.ComponentsTest do
</form>
"""
end

test "method is case insensitive when using get or post with action" do
assigns = %{}

template = ~H"""
<.form for={%{}} method="GET" action="/"></.form>
"""

assert t2h(template) ==
~x{<form method="get" action="/"></form>}

template = ~H"""
<.form for={%{}} method="PoST" action="/"></.form>
"""

csrf = Plug.CSRFProtection.get_csrf_token_for("/")

assert t2h(template) ==
~x{<form method="post" action="/"><input name="_csrf_token" type="hidden" hidden="hidden" value="#{csrf}"></form>}

# for anything != get or post we use post and set the hidden _method field
template = ~H"""
<.form for={%{}} method="PuT" action="/"></.form>
"""

assert t2h(template) ==
~x"""
<form action="/" method="post">
<input name="_method" type="hidden" hidden="hidden" value="PuT">
<input name="_csrf_token" type="hidden" hidden="hidden" value="#{csrf}">
</form>
"""
end
end

describe "inputs_for" do
Expand Down

0 comments on commit e5c44d8

Please sign in to comment.