Phoenix LiveView -- a form without database?

Trying to get into Phoenix with LiveView. I haven’t been able to find a demo or sample code containing a simple form without a database or Ecto.

Do I need to make Ecto changesets from maps or such?

I’m just looking wire a form submission with some textareas to a couple fields on socket assigns.

1 Like

You don’t need a changeset. You can do something like this:

<%= f = form_for :user, "#", [phx_submit: "submit"] %>
  <%= text_input f, :name %>
</form>
6 Likes

That’s about what I’d been trying – though using a map or such for the :user, I get
protocol Phoenix.HTML.Safe not implemented

(and same with Changesets now… hrm)

Make sure that you have the latest phoenix_html in your dependencies.

{:phoenix_html, "~> 2.13"}`
2 Likes

Keep in mind that Ecto changesets do not need to be tied to a database at all. All of our live view forms ultimately make GraphQL API calls, but we still use changesets for the convenience of providing “client side” validation. Then we just Ecto.Changeset.apply_changes on a valid changeset, and take the resulting map and send it over the network to the API.

4 Likes

Thanks for the help! I upgraded and cleaned everything. But It turned out that that error was from pushing it against a different property in the assigns map.

Progress: I can now submit all my values using that setup!

But they don’t display on mount or after assigning them from the phx_submit event.
I can display other liveview values though by using that event to apply things in the leex like
<%= @some_value %>.

Thanks again!

1 Like

To show the mount I was using with it:

def mount(_session, socket) do
    {:ok, assign(socket, code: %{code_content: "meow1", code_error: "meow2"})}
end
1 Like

I’ve done this with changesets and phoenix_html. Works awesome and does not need to be tied to a database at all. Very fast and flexible.

3 Likes