formex v0.3.2 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"
])
|> add_button(:reset, "Reset form", phoenix_opts: [
class: "btn-default"
])
|> add_button(:submit, if(form.struct.id, do: "Edit", else: "Add"), phoenix_opts: [
class: "btn-primary"
])
end
# optional
def changeset_after_create_callback(changeset) do
# do an extra validation
changeset
end
end
Summary
Callbacks
In this callback you have to add fields to the form
Callback that will be called after changeset creation. In this function you can for example add an extra validation to your changeset
Functions
Adds a field to the 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.
Adds a button to the form.
The type
may be either :submit
or :reset
.
This function invokes Formex.Button.create_button/3
.
Callbacks
In this callback you have to add fields to the form.
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 an extra validation to your changeset.