phoenixframework / phoenix_live_view

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

Empty string checkbox value considered invalid during test #3258

Closed davydog187 closed 4 months ago

davydog187 commented 4 months ago

Environment

Actual behavior

I have a form defined like so. The underlying data type is an array of Ecto.Enum

    field :products, {:array, Ecto.Enum}, values: @products, default: []

The form looks like this

<.form for={@products_form} phx-change="change_products" id="products-form">
  <.input
    type="checkbox"
    name="organization[products][access]"
    value={Enum.member?(@products_form[:products].value, :access)}
    label="Access"
  />
  <.input
    type="checkbox"
    name="organization[products][reporting]"
    value={Enum.member?(@products_form[:products].value, :reporting)}
    label="Reporting"
  />
</.form>

When we get the params during change events, the checked boxes from the page load will come back as empty strings like so

products #=> %{"access" => "", "reporting" => "false"}

When writing a LiveView test, this is treated as an invalid value for a checkbox

     ** (ArgumentError) value for checkbox "organization[products][access]" must be one of ["false", "true"], got: ""
     code: |> render_change()
     stacktrace:
       (phoenix_live_view 0.20.11) lib/phoenix_live_view/test/live_view_test.ex:1102: Phoenix.LiveViewTest.call/2
       test/myapp_web/live/page_live_test.exs:260: (test)

Expected behavior

I see that this is either a problem with the Javascript or the correct set of possible values in test. So the expected behavior is either:

  1. Checkboxes always return "true" or "false" as values during change events
  2. Empty string "" is a valid checkbox value during tests
davydog187 commented 4 months ago

This was an (admittedly confusing) user error, closing