(I am not seeing a straightforward way to do this – not without Javascript anyway – but I figured I’d ask, just in case.)
I want to alter my Phoenix HTML form, depending on the value of one of its fields, whenever that value changes.
In the silly non-working example below, the form’s text changes whenever the :answer
field selection changes. This pseudocode “accomplishes” this with the help of the magical function evaluate_on_every_form_data_change(current_field_values)
, which “computes” the appropriate text every time a field on the form changes:
app.html.heex
:
<main class="container">
...
<%= form_for @conn, Routes.api_controller_item_path(@conn, :new_item), fn f -> %>
<%= select f, :answer, ["Yes!!": "yes", "Nope": "no"], selected: "yes" %>
<%= evaluate_on_every_form_data_change(f, fn current_field_values -> %>
<%= if current_field_values.answer == "yes" do %>
<%= "OMG YAY" %>
<% else %>
<%= "SAD" %>
<% end %>
<% end ) %>
<%= submit "Submit" %>
<% end %>
...
Obviously, evaluate_on_every_form_data_change(form_data)
is not a thing, but if anyone has any suggestions / pointers (or can confirm the futility of this endeavor;), I’m super-interested.
Thank you!
Mark.
EDIT: to clarify, this is NOT a LiveView form.