Skip to content

Commit 4cecfad

Browse files
authored
Use checkbox default value 'on' (#3886)
1 parent 26bb662 commit 4cecfad

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

lib/phoenix_live_view/test/client_proxy.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,7 @@ defmodule Phoenix.LiveViewTest.ClientProxy do
14421442

14431443
defp form_defaults("input", node, name, acc) do
14441444
type = TreeDOM.attribute(node, "type") || "text"
1445-
value = TreeDOM.attribute(node, "value") || ""
1445+
value = TreeDOM.attribute(node, "value") || default_value(type)
14461446

14471447
cond do
14481448
type in ["radio", "checkbox"] ->
@@ -1568,7 +1568,7 @@ defmodule Phoenix.LiveViewTest.ClientProxy do
15681568
type = TreeDOM.attribute(node, "type") || "text"
15691569

15701570
if type in ["radio", "checkbox", "hidden"] do
1571-
value = TreeDOM.attribute(node, "value") || ""
1571+
value = TreeDOM.attribute(node, "value") || default_value(type)
15721572
{[type | types], [value | values]}
15731573
else
15741574
{[type | types], values}
@@ -1592,6 +1592,9 @@ defmodule Phoenix.LiveViewTest.ClientProxy do
15921592
{types, values}
15931593
end
15941594

1595+
defp default_value("checkbox"), do: "on"
1596+
defp default_value(_type), do: ""
1597+
15951598
defp fill_in_name("", name), do: name
15961599
defp fill_in_name(prefix, name), do: prefix <> "[" <> name <> "]"
15971600

test/phoenix_live_view/integrations/elements_test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,11 @@ defmodule Phoenix.LiveView.ElementsTest do
784784
end
785785
end
786786

787+
test "fill in checkbox without value (default: on)", %{live: view} do
788+
assert view |> form("#form", hello: [checkbox_no_value: "on"]) |> render_change
789+
assert last_event(view) =~ ~s|"checkbox_no_value" => "on"|
790+
end
791+
787792
test "fill in multiple checkbox", %{live: view} do
788793
assert_raise ArgumentError,
789794
"value for checkbox \"hello[multiple-checkbox][]\" must be one of [\"1\", \"2\", \"3\"], got: \"unknown\"",

test/support/live_views/elements.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ defmodule Phoenix.LiveViewTest.Support.ElementsLive do
161161
<input name="hello[checkbox]" type="checkbox" value="1" />
162162
<input name="hello[checkbox]" type="checkbox" value="2" checked />
163163
<input name="hello[checkbox]" type="checkbox" value="3" />
164+
<input name="hello[checkbox_no_value]" type="checkbox" />
164165
<input name="hello[not-checked-checkbox]" type="checkbox" value="1" />
165166
<input name="hello[disabled-checkbox]" type="checkbox" value="1" checked disabled />
166167
<input name="hello[multiple-checkbox][]" type="checkbox" value="1" />

0 commit comments

Comments
 (0)