formex v0.3.2 Formex.Template behaviour

Use this module to create a custom form template.

Usage:

defmodule App.FormTemplate.SemanticUI do
  use Formex.Template

  def generate_row(form, field, _options \\ []) do
    # code that produces a Phoenix.HTML.safe
    # you can use here render_phoenix_input/2, translate_error/2, has_error/2
  end
end

If you want to create a several version of template (for example vertical and horizontal, as is already done with Bootstrap), you can move a common code to another module.

Example:

defmodule App.FormTemplate.SemanticUIVertical do
  use Formex.Template, :main # note the :main argument
  import App.FormTemplate.SemanticUI

  def generate_row(form, field, _options \\ []) do
    # code that produces a Phoenix.HTML.safe
  end
end
defmodule App.FormTemplate.SemanticUIHorizontal do
  use Formex.Template, :main # note the :main argument
  import App.FormTemplate.SemanticUI

  def generate_row(form, field, _options \\ []) do
    # code that produces a Phoenix.HTML.safe
  end
end
defmodule App.FormTemplate.SemanticUI do
  use Formex.Template, :helper # note the :helper argument

  # a common code for both versions
  # you can use here render_phoenix_input/2, translate_error/2, has_error/2
end

Check the source code of templates for more examples.

Summary

Functions

Adds a CSS class to a :phoenix_opts keyword

Checks if given field has a changeset error

Runs function from Phoenix.HTML.Form defined in a Field.type or Button.type

Translates error using function set in :formex config

Callbacks

Generates a HTML for a field

Functions

add_class(phoenix_opts, class)
add_class(phoenix_opts :: Keyword.t, class :: String.t) :: Keyword.t

Adds a CSS class to a :phoenix_opts keyword

has_error(form, field)
has_error(Formex.Form.t, Formex.Field.t) :: any

Checks if given field has a changeset error

render_phoenix_input(item, args)
render_phoenix_input(item :: any, args :: Keyword.t) :: any

Runs function from Phoenix.HTML.Form defined in a Field.type or Button.type

translate_error(form, field)
translate_error(Formex.Form.t, Formex.Field.t) :: any

Translates error using function set in :formex config

Callbacks

generate_row(form, field, options)
generate_row(form :: Formex.Form.t, field :: Formex.Field.t, options :: Keyword.t) :: Phoenix.HTML.safe

Generates a HTML for a field.

Arguments

  • options - any options that you want to use inside a form template. It can be set by :template_options inside a Formex.View functions, or in the :formex config. For example, Formex.Template.BootstrapHorizontal uses options that stores columns sizes.