Working Example of Phoenix Built in Form Component

From ElixirBlocks
Jump to: navigation, search



defmodule AppWeb.PageLive do
   use AppWeb, :live_view  
   def mount(_params, _session, socket)  do
     {:ok, assign(socket, form: to_form(%{}, as: :my_form))}
   end

   def handle_event("save", params, socket) do
     IO.inspect params
     {:noreply, socket}
   end
   
   def handle_event("validate", params, socket) do
      IO.inspect params
      {:noreply, socket}
    end

   def render(assigns) do
     ~H"""
        <.form for={@form} phx-change="validate" phx-submit="save">
           <.input type="text" field={@form[:username]} />
           <.input type="email" field={@form[:email]} />
           <button>Save</button>
        </.form>
	 """ 
   end

end