With the form below, when I start typing into a field, the content of the other one gets cleared. Why is that? How do I prevent LiveView from clearing the input fields? Is it possible to do without a changeset?
defmodule HelloWeb.FormLive do
use HelloWeb, :live_view
def mount(_params, _session, socket) do
{:ok, assign(socket, :form_params, "")}
end
def render(assigns) do
~L"""
<%= f = form_for :user, "#", [phx_change: :validate, phx_submit: :save] %>
<%= label f, :username %>
<%= text_input f, :username %>
<%= error_tag f, :username %>
<%= label f, :email %>
<%= text_input f, :email %>
<%= error_tag f, :email %>
<%= submit "Save" %>
</form>
<p>Form params:</p>
<pre>
<%= inspect(@form_params) %>
</pre>
"""
end
def handle_event("validate", %{"user" => user}, socket) do
{:noreply, assign(socket, :form_params, user)}
end
end