formex v0.2.0 Formex.Type behaviour

In order to create a form, you need to create the Type file. It’s similar to Symfony’s way of creating forms.

Example:

defmodule App.ArticleType do
  use Formex.Type
  alias Formex.CustomField.SelectAssoc

  def build_form(form) do
    form
    |> add(:text_input, :title, label: "Title")
    |> add(:textarea, :content, label: "Content", phoenix_opts: [
      rows: 4
    ])
    |> add(:checkbox, :hidden, label: "Is hidden", required: false)
    |> add(SelectAssoc, :category_id, label: "Category", phoenix_opts: [
      prompt: "Choose category"
    ])
  end

  # optional
  def changeset_after_create_callback(changeset) do
    # do extra validation and return a new changeset
    changeset
  end
end

Summary

Callbacks

Adds a field to a form

In this callback you have to add fields to a Form

Callback that will be called after changeset creation. In this function you can for example add extra validation to your changeset

Callbacks

add(form, type_or_module, name, opts)
add(form :: Form.t, type_or_module :: Atom.t, name :: Atom.t, opts :: Map.t) :: Form.t

Adds a field to a form.

If the type_or_module is an atom, then this function invokes Formex.Field.create_field/4. Otherwise, the Formex.CustomField.create_field/3 is called.

build_form(form)
build_form(form :: Formex.Form.t) :: Formex.Form.t

In this callback you have to add fields to a Form.

changeset_after_create_callback(changeset)
changeset_after_create_callback(changeset :: Ecto.Changeset.t) :: Ecto.Changeset.t

Callback that will be called after changeset creation. In this function you can for example add extra validation to your changeset.