formex v0.4.14 Formex.Controller

Helpers for controller. Imports Formex.Builder.

Installation:

web/web.ex

def controller do
  quote do
    use Formex.Controller
  end
end

Usage

CRUD

def new(conn, _params) do
  form = create_form(App.ArticleType, %Article{})
  render(conn, "new.html", form: form)
end
def create(conn, %{"article" => article_params}) do
  App.ArticleType
  |> create_form(%Article{}, article_params)
  |> insert_form_data
  |> case do
    {:ok, _article} ->
      # ...
    {:error, form} ->
      # ...
  end
end
def edit(conn, %{"id" => id}) do
  article = Repo.get!(Article, id)
  form = create_form(App.ArticleType, article)
  render(conn, "edit.html", article: article, form: form)
end
def update(conn, %{"id" => id, "article" => article_params}) do
  article = Repo.get!(Article, id)

  App.ArticleType
  |> create_form(article, article_params)
  |> update_form_data
  |> case do
    {:ok, article} ->
      # ...
    {:error, form} ->
      # ...
  end
end

Usage without a database

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

  embedded_schema do # instead of `schema`
    field :email
    field :password
  end
end
def register(conn, %{"registration" => registration_params}) do
  RegistrationType
  |> create_form(%Registration{}, registration_params)
  |> handle_form
  |> case do
    {:ok, registration} ->
      # do something with the `registration`
    {:error, form} ->
      # display errors
      render(conn, "index.html", form: form)
  end
end

Summary

Functions

Works similar to insert_form_data/1 and update_form_data/1, but doesn’t require a database. Should be used with embedded_schema

Invokes Repo.insert. In case of :error, returns {:error, form} (with new form.changeset value) instead of {:error, changeset} (as Ecto does)

Invokes Repo.update. In case of :error, returns {:error, form} (with new form.changeset value) instead of {:error, changeset} (as Ecto does)

Functions

handle_form(form)
handle_form(Formex.Form.t) ::
  {:ok, Ecto.Schema.t} |
  {:error, Formex.Form.t}

Works similar to insert_form_data/1 and update_form_data/1, but doesn’t require a database. Should be used with embedded_schema.

insert_form_data(form)
insert_form_data(Formex.Form.t) ::
  {:ok, Ecto.Schema.t} |
  {:error, Formex.Form.t}

Invokes Repo.insert. In case of :error, returns {:error, form} (with new form.changeset value) instead of {:error, changeset} (as Ecto does)

update_form_data(form)
update_form_data(Formex.Form.t) ::
  {:ok, Ecto.Schema.t} |
  {:error, Formex.Form.t}

Invokes Repo.update. In case of :error, returns {:error, form} (with new form.changeset value) instead of {:error, changeset} (as Ecto does)