Formex v0.6.7 Formex.Field



Defines the Formex.Field struct


t() :: %Formex.Field{custom_value: term, data: term, label: term, name: term, opts: term, phoenix_opts: term, required: term, struct_name: term, type: term, validation: term}



Defines the Formex.Field struct.

  • :name - a field name, for example: :title
  • :struct_name - a name of a key in your struct. By default the same as :name
  • :custom_value - custom function that extracts a value that will be used in view
  • :type - a type of a field that in most cases will be the name of a function from Phoenix.HTML.Form
  • :value - the value from struct/params
  • :required - is field required? Used only in template, not validated
  • :validation - validation rules to be passed to a validator
  • :label - the text label
  • :data - additional data used by particular field type (eg. :select stores here data for <option>’s)
  • :opts - options
  • :phoenix_opts - options that will be passed to Phoenix.HTML.Form
create_field(type, name, opts \\ [])

Creates a new field.

type is the name of function from Phoenix.HTML.Form. For example, if you need to render a password field, then use Phoenix.HTML.Form.password_input/3 in that way:

|> add(:pass, :password_input)


  • :label
  • :required - defaults to true. Used only by the template helper to generate an additional .required CSS class.
  • :struct_name - a name of a key in your struct. Defaults to the name variable
  • :custom_value - use this, if you need to change value that will be used in view. For example, field of Money.Ecto.Type type casted to string returns a formatted number, when we may need a raw number. In this case we should use:

    |> add(:money, :text_input, custom_value: fn value ->
      if value do
  • :phoenix_opts - options that will be passed to Phoenix.HTML.Form, for example:

    |> add(:content, :textarea, phoenix_opts: [
      rows: 4

Options for <select>

  • :choices - list of <option>s. Named “choices”, not “options”, because we don’t want to confuse it with the rest of options

      |> add(:field, :select, choices: ["Option 1": 1, "Options 2": 2])
      |> add(:country, :select, choices: [
        "Europe": ["UK": 1, "Sweden": 2, "Poland": 3],
        "Asia": [...]
  • :without_choices - set this option to true if you want to render select without any <option>s and provide them in another way (for example, using Ajax-Bootstrap-Select).

    It disables choices rendering in Formex.Ecto.CustomField.SelectAssoc.

  • :choice_label_provider - used along with :select_without_choices.

    When form is sent but it’s displayed again (because of some errors), we have to render <select> with a single <option>, previously chosen by user.

    This option expects a function that receives id and returns some label.

      |> add(:customer, :select, without_choices: true, choice_label_provider: fn id ->
        Repo.get(Customer, id).name

    Formex.Ecto.CustomField.SelectAssoc will set this option for you