Formex - a form library for Phoenix inspired by Symfony

Now is possible to use embedded_schema instead of schema, it means, there is no database requirement (but still Ecto is required).

I know that using embedded schemas in that way is kinda hack, so I’m not proud of it. But it was very easy to implement.

Example:

defmodule App.Registration do
  use App.Web, :model

  embedded_schema do # instead of `schema "table_name"`
    field :email
    field :password
  end
end
defmodule App.RegistrationType do
  use Formex.Type

  def build_form(form) do
    form
    |> add(:email, :text_input, label: "E-mail")
    |> add(:password, :password_input, label: "Password")
    |> add(:save, :submit, label: "Register")
  end
end

Controller

def register(conn, %{"registration" => registration_params}) do
  RegistrationType
  |> create_form(%Registration{}, registration_params)
  |> handle_form # new function
  |> case do
    {:ok, registration} ->
      # do something with the `registration`
    {:error, form} ->
      # display errors
      render(conn, "index.html", form: form)
  end
end
3 Likes