Phx_trigger_action is not working in LiveView

Hi I am using LiveView for admin login page
form looks like this

 <%= f = form_for @changeset,
              Routes.admin_auth_path(@socket, :log_in),
              phx_submit: "save",
              phx_trigger_action: @trigger_submit  %>

   <%= email_input f, :email, required: true, placeholder: "Email address" %>
   <%= password_input f, required: true,  placeholder: "Password" %>
  <%= submit "Sign in" %>
</form>

and handle_event function

def handle_event("save", %{"admin_user" => params}, socket) do
    %{"email" => email, "password" => password} = params

    case AdminAccounts.get_user_by_email_and_password(email, password) do
      nil ->
        changeset = AdminUser.changeset(%AdminUser{}, %{email: email, password: password})

        {:noreply,
         assign(
           socket,
           changeset: changeset,
           trigger_submit: false,
           error_message: "Failed to login"
         )}

      %AdminUser{} = _admin_user ->
        changeset = AdminUser.changeset(%AdminUser{})
        IO.puts("Found admin user")

        {:noreply,
         assign(
           socket,
           changeset: changeset,
           trigger_submit: true,
           error_message: nil
         )}
    end
  end

The problem is that It was WORKING OK last night. But today in the morning it is not working.
When I submit the form, it found correct admin user and set trigger_submit assigns to true.
But it didn’t trigger controller action, just refresh the page.

What I did last night :frowning:

  1. Updated phoenix liveview to latest release 0.15.4 and phoenix 1.5.7
  2. Setup to Integrate alpinejs, add this code to app.js
let liveSocket = new LiveSocket("/live", Socket, {
  uploaders: Uploaders,
# This code added.
  dom: {
    onBeforeElUpdated(from, to) {
      if (from.__x) {
        window.Alpine.clone(from.__x, to);
      }
    },
  },
  params: { _csrf_token: csrfToken },
});

And I inspect form element in rendered html page but I can’t see phx_trigger_action attribute

<form action="/staff-login"  method="post" phx-submit="save">
   <input name="_csrf_token" type="hidden" value="some token value">
   ....
 </form>

Is this normal?

Can you help me to find any clue to my problem?

I just found what problem was.
First, it was not the problem with phx_trigger_action. My form triggers correctly.
I passed empty changeset to liveview in success case
changeset = AdminUser.changeset(%AdminUser{})
I think that is clear the form value and in controller action, I have empty params.(No email and password passed)
So I changed

def handle_event("save", %{"admin_user" => params}, socket) do
    %{"email" => email, "password" => password} = params
    IO.inspect(params)

    case AdminAccounts.get_user_by_email_and_password(email, password) do
      nil ->
        changeset = AdminUser.changeset(%AdminUser{}, %{email: email, password: password})

        {:noreply,
         assign(
           socket,
           changeset: changeset,
           trigger_submit: false,
           error_message: "Failed to login"
         )}

      %AdminUser{} = _admin_user ->
        changeset = AdminUser.changeset(%AdminUser{}, %{email: email, password: password})
        IO.puts("Found admin user")

        {:noreply,
         assign(
           socket,
           changeset: changeset,
           trigger_submit: true,
           error_message: nil
         )}
    end
  end

And in the password form field. I just added
value: input_value(f, :password)

 <%= password_input f, :password, required: true, placeholder: "Password",
                                     value: input_value(f, :password) %>
1 Like