Formex v0.6.7 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, 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, has_error/2
end

See the source code of templates for more examples.

Summary

Functions

Adds a CSS class to the :phoenix_opts keyword

Returns list of errors

Checks if given field has an error

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

Functions

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

Adds a CSS class to the :phoenix_opts keyword

format_error(error)

Formats error using Phoenix.HTML.Format.text_to_html/2

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

Returns list of errors

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

Checks if given field has an 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

Callbacks

generate_input(form, field_or_button)
generate_input(form :: Formex.Form.t, field_or_button :: any) :: Phoenix.HTML.safe
generate_label(form, field, class)
generate_label(form :: Formex.Form.t, field :: Formex.Field.t, class :: String.t) :: Phoenix.HTML.safe
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.