Patterns for displaying errors to users with LiveView

I’m wondering if anyone has come up with good strategies for showing errors to users when using LiveView.

By default app.html.eex can show flash messages. Are people duplicating these in a layout-like live template? Redirecting and using put_flash? Something else?

@marcdel For folks who are happy with flash messages today, live view ships with a live flash implementation that does basically the same thing, no redirect required.

4 Likes

phoenix_live_view.ex#L835

Is this the one you mean?

The docs seem to suggest that it requires a redirect (though it looks like that can be a live_path). Any chance you’ve got an example you could point me to?

1 Like

Flash is nice for redirecting away from the LV, but for showing the user feedback as they are interacting with your LV, you can simply lean on the existing features:

def render(assigns) do
  ~L"""
  <%= if @notice do %>
    <h2><%= @notice %>
  <% end %>
  ...
  """
end

def mount(_, socket) do
  {:ok, assign(socket, ..., notice: nil)}
end
...
def handle_event(...) do
  case some_thing(...) do
    ... ->
    {:error, _} -> {:noreply, assign(socket, :notice, "it didn't work")}
  end
end

That’s all there is to it, no extra pattern required :slight_smile:

11 Likes