Formex v0.6.7 Formex.View

Helper functions for templating.

Example of use:

<%= formex_form_for @form, @action, fn f -> %>
  <%= if @form.submitted? do %>
    <div class="alert alert-danger">
      <p>Oops, something went wrong! Please check the errors below.</p>
    </div>
  <% end %>

  <%= formex_rows f %>

  <div class="form-group">
    <%= submit "Submit", class: "btn btn-primary" %>
  </div>
<% end %>

Changing a form template

You can change the template globally or in the specific form/field.

  • config

    config :formex,
      template: Formex.Template.BootstrapHorizontal
      template_options: [ # options used by this template
        left_column: "col-xs-2",
        right_column: "col-xs-10"
      ]
  • formex_form_for/4:

    <%= formex_form_for @form, @action, [
        class: "form-horizontal",
        template: Formex.Template.BootstrapHorizontal
      ], fn f -> %>
      ...
    <% end %>
  • formex_rows/2:

    <%= formex_rows f, template: Formex.Template.BootstrapHorizontal %>
  • formex_row/3:

    <%= formex_row f, :name, template: Formex.Template.BootstrapHorizontal %>

Summary

Functions

formex_form_for(form, action, options \\ [], fun)
formex_form_for(form :: Formex.Form.t, action :: String.t, options :: Keyword.t, fun :: (Formex.t -> Phoenix.HTML.unsafe)) :: Phoenix.HTML.safe

Works similar to a Phoenix.HTML.Form

In the callback function the first argument is Formex.Form.t/0 instead of a Phoenix.HTML.Form.t/0. This argument contains the Phoenix.HTML.Form.t/0 under a :phoenix_form key

Options

In options argument you are passing together options for Formex.View and for Phoenix.HTML.

Formex options

Phoenix options

Options not mentioned before will be passed to a Phoenix.HTML.Form function. Options below are already set by Formex and can be overriden.

  • as - form name, defaults to struct name
  • method - method, defaults to :post

For rest of options, see Phoenix.HTML.Form docs.

formex_input(form, item_name, options \\ [])

Generates an input, used by formex_row/3

Example of use:

<div>
  <%= formex_label f, :title %>
  <%= formex_input f, :title %>
</div>

<%= formex_input f, :some_hidden_field %>

Options

formex_label(form, item_name, options \\ [])

Generates a label, used by formex_row/3

Example of use:

<div>
  <%= formex_label f, :title %>
  <%= formex_input f, :title %>
</div>

Options

formex_row(form, item_name, options \\ [])

Generates a row using formex_label/3 and formex_input/3

Example of use:

<%= formex_row f, :title %>
<%= formex_row f, :content %>
<%= formex_row f, :category_id %>

Options

formex_rows(form, options \\ [])

Generates all formex_row/2s at once

Options

get_template(form, row_options)
get_template_options(form, row_options)