Any strategies with LiveView to avoid typos of magic string event names?

Module attributes can’t be accessed inside HEEX since the @ operator is used by the engine to access socket.assigns.

You’d need to do:

defmodule HelloWeb.UserLive.FormComponent do
  use HelloWeb, :live_component

  @reset_password_event "reset_password"

  # Use in the render:
  def render(assigns) do
    ~H"""
      <.simple_form
        for={@form}
        id="user-form"
        phx-target={@myself}
        phx-change={reset_password_event()}
        phx-submit="save"
      >
      # ...
    """
  end

  # And in the handler:
  def handle_event(@reset_password_event, %{"user" => user_params}, socket) do
    # ...
  end

  defp reset_password_event(), do: @reset_password_event
end
7 Likes