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:
<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 %> ...
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.
EDIT: to clarify, this is NOT a LiveView form.