Is there a simple solution for the following problem, which I’m unable to figure out:
-
On initial page render the modal dialog is hidden
-
On button click the modal dialog is shown using:
{:noreply,
push_event(
socket
|> assign(:meal_in_editor, content)
|> assign(:changeset_meal, Meal.changeset_quick_edit(content, %{})),
"showModal",
%{id: "modal-edit"}
)}
- showModal is just a few lines of javascript:
window.addEventListener("phx:showModal", (e) => {
var element = document.getElementById(e.detail.id);
var elementBG = document.getElementById(e.detail.id + "-bg");
var elementContainer = document.getElementById(e.detail.id + "-container");
element.style.display = "block";
elementBG.style.display = "block";
elementContainer.style.display = "block";
});
-
form validation is enabled using
phx-change="validate-edit"
-
handling the event like this:
def handle_event("validate-edit", %{"form" => params}, %{assigns: %{meal_in_editor: meal}} = socket) do
form =
meal
|> Meal.changeset_edit(params)
|> to_form()
{:noreply, assign(socket, changeset: form)}
end
- as soon as the changeset is updated the modal dialog gets rerendered (in its initial hidden state) thus its getting closed
How to keep the modal dialog open during validation?