Skip to content

Cope with conditionally rendered form inputs#297

Open
s3cur3 wants to merge 2 commits intogermsvel:mainfrom
s3cur3:ty/conditional-fields
Open

Cope with conditionally rendered form inputs#297
s3cur3 wants to merge 2 commits intogermsvel:mainfrom
s3cur3:ty/conditional-fields

Conversation

@s3cur3
Copy link
Contributor

@s3cur3 s3cur3 commented Jan 30, 2026

This fixes an issue with fill_in where past input values were accumulated and then passed to LiveViewTest.render_change/2 even when the input that would have produced the value was no longer present in the form. (The change simply removes inputs whose name is no longer present in the form when using fill_in.)

On a complicated form, you might have some inputs (say, a <select>) which conditionally hide other elements on the form. (One could argue that the unused inputs should merely be hidden, rather than removed from rendering in such cases, but on a sufficiently complicated form, it sometimes makes sense to truly remove the inputs.)

Prior to these changes, the newly added test would fail with a message like this:

  1) test conditional form fields submitting after switching versions only includes the visible field (PhoenixTest.LiveTest)
     test/phoenix_test/live_test.exs:1458
     ** (ArgumentError) could not find non-disabled input, select or textarea with name "version_a_text" within:

     <select id="version" name="version"><option value="a">Version A</option><option value="b">Version B</option></select><input id="version_b_text" name="version_b_text"/>
     code: |> fill_in("Version B Text", with: "some value for B")
     stacktrace:
       (phoenix_live_view 1.1.8) lib/phoenix_live_view/test/live_view_test.ex:1122: Phoenix.LiveViewTest.call/2
       (phoenix_test 0.9.1) lib/phoenix_test/live.ex:460: PhoenixTest.Live.trigger_form_phx_change/3
       test/phoenix_test/live_test.exs:1463: (test)

As a user, that error was quite confusing, because I said to myself "Well that doesn't make sense! I was trying to fill in 'Version B Text,' not version_a_text like the error message says!"

(With this change, that test simply passes.)

This fixes an issue with `fill_in` where past input values were accumulated and then passed to `LiveViewTest.render_change/2` even when the input that would have produced the value was no longer present in the form. The change simply removes inputs whose `name` is no longer present in the form when using `fill_in`.

On a complicated form, you might have some inputs (say, a `<select>`) which conditionally hide other elements on the form. (One could argue that the unused inputs should merely be hidden, rather than removed from rendering in such cases, but on a sufficiently complicated form, it sometimes makes sense to truly remove the inputs.)

Prior to these changes, the newly added test would fail with a message like this:

```
  1) test conditional form fields submitting after switching versions only includes the visible field (PhoenixTest.LiveTest)
     test/phoenix_test/live_test.exs:1458
     ** (ArgumentError) could not find non-disabled input, select or textarea with name "version_a_text" within:

     <select id="version" name="version"><option value="a">Version A</option><option value="b">Version B</option></select><input id="version_b_text" name="version_b_text"/>
     code: |> fill_in("Version B Text", with: "some value for B")
     stacktrace:
       (phoenix_live_view 1.1.8) lib/phoenix_live_view/test/live_view_test.ex:1122: Phoenix.LiveViewTest.call/2
       (phoenix_test 0.9.1) lib/phoenix_test/live.ex:460: PhoenixTest.Live.trigger_form_phx_change/3
       test/phoenix_test/live_test.exs:1463: (test)
```

As a user, that error was quite confusing, because I said to myself "Well that doesn't make sense! I was trying to fill in 'Version B Text,' not `version_a_text` like the error message says!"

(With this change, that test simply passes.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments