Hi @boiserunner can you show more of your code? You aren’t showing how the "edit" event itself is triggered. Are you sure that the function is being called?
def render(assigns)
~H"""
<.form for={@form} phx-submit="test">
<.input field={@form[:title]} />
<button type="submit">Update the form!</button>
</.form>
"""
end
Edit:
Oh, you’re also not using the params when building the changeset in the handle_event callback:
def handle_event("test", params, socket) do
challenge = App.Repo.get!(Challenge, 1)
changeset = Challenge.changeset(challenge, params) # likely needs to be: params["something"] but I can't tell what the "something" is from your code
{:noreply, assign(socket, :form, to_form(changeset))}
end
Yes and no. The form itself isn’t triggering the event to update the form (which may very well be the problem). Instead, the user clicks a button to load a record, and now my form is in an EDIT context. So I have no form values coming in when the initial event is triggered.
I’m not sure yet why a re-render is not sufficient. I’ll take a closer look at to_form and see if the re-render relies on changed values rather than the underlying data.
Are you sure the form input is not focused, which prevents phoenix from updating the form? You can test this by at the same time as updating the underlying data updating an id on the form.
Because you usually don’t want to switch out the context underneight users feets while they’re editing the input. Especially not when a roundtrip to the server might be involved.
Haven’t tested this yet, but a potential alternative to bumping the id would be to explicitly set the focus on click. LiveView also includes nifty JS Commands that can help manipulate focus.
For example: <button phx-click={JS.focus() |> JS.push("test")}>Update the form!</button>
There’s also a JS.push_focus() and corresponding JS.pop_focus() command for saving and restoring focus. If there are various buttons that update the form differently, this could be potentially very useful to explicitly set different focuses depending on the update applied.