Packages
formex
0.2.1
0.6.7
0.6.6
0.6.5
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.10
0.5.9
0.5.8
0.5.7
0.5.6
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.16
0.4.15
0.4.14
0.4.13
0.4.12
0.4.11
0.4.10
0.4.9
0.4.8
0.4.7
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.3
0.2.2
0.2.1
0.2.0
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
Form library for Phoenix with Ecto support
Current section
Files
Jump to
Current section
Files
lib/type.ex
defmodule Formex.Type do
@moduledoc """
In order to create a form, you need to create the Type file. It's similar to
[Symfony's](https://symfony.com/doc/current/forms.html#creating-form-classes)
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 an extra validation
changeset
end
end
```
"""
defmacro __using__([]) do
quote do
@behaviour Formex.Type
def changeset_after_create_callback( changeset ) do
changeset
end
def add(form, type_or_module, name, opts) do
# check if type_or_module is atom or module
field = if :erlang.function_exported(type_or_module, :module_info, 0) do
type_or_module.create_field(form, name, opts)
else
Formex.Field.create_field(form, type_or_module, name, opts)
end
Formex.Form.put_field(form, field)
end
defoverridable [changeset_after_create_callback: 1]
end
end
@doc """
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 `c:Formex.CustomField.create_field/3` is called.
"""
@callback add(form :: Form.t, type_or_module :: Atom.t, name :: Atom.t, opts :: Map.t) :: Form.t
@doc """
In this callback you have to add fields to the form.
"""
@callback build_form(form :: Formex.Form.t) :: Formex.Form.t
@doc """
Callback that will be called after changeset creation. In this function you can
for example add an extra validation to your changeset.
"""
@callback changeset_after_create_callback(changeset :: Ecto.Changeset.t) :: Ecto.Changeset.t
end