Skip to main content

Form helper

The l_ui_form helper renders a complete form from a record and a field configuration array. It handles labels, inputs, hints, validation errors, and the submit button.

Basic form

Pass a record, an array of field hashes, and a submission URL. Field types are inferred from the model's column types when as: is not specified.

Supports plain text only

<%= l_ui_form(@post, fields: [
      { attribute: :title, required: true },
      { attribute: :body, hint: "Supports plain text only" },
    ],
    url: posts_path) %>

All field types

Use as: to explicitly set the field type. Supported types: :string, :text, :email, :password, :number, :tel, :url, :search, :date, :datetime, :time, :month, :week, :color, :range, :file, :select, :checkbox, and :hidden. The form is automatically rendered as multipart when any field uses :file.

A longer description

<%= l_ui_form(@post, fields: [
      { attribute: :title, as: :string, required: true,
        placeholder: "Enter a title" },
      { attribute: :body, as: :text,
        hint: "A longer description" },
      { attribute: :user_id, as: :select, label: "Author",
        prompt: "Choose an author",
        collection: User.pluck(:name, :id) },
      { attribute: :email, as: :email },
      { attribute: :password, as: :password },
      { attribute: :age, as: :number },
      { attribute: :phone, as: :tel },
      { attribute: :website, as: :url },
      { attribute: :query, as: :search },
      { attribute: :starts_on, as: :date },
      { attribute: :starts_at, as: :datetime },
      { attribute: :starts_time, as: :time },
      { attribute: :starts_month, as: :month },
      { attribute: :starts_week, as: :week },
      { attribute: :tint, as: :color },
      { attribute: :volume, as: :range },
      { attribute: :avatar, as: :file },
      { attribute: :accepted, as: :checkbox },
      { attribute: :secret_id, as: :hidden },
    ],
    url: posts_path) %>

Options

l_ui_form options
Option Type Description
record ActiveRecord The model instance (first positional argument)
fields: Array Array of field hashes (see field options below)
url: String Form submission URL
method: Symbol HTTP method override (defaults to POST for new records, PATCH for persisted)
submit: String Submit button text (defaults to "Create" for new records, "Save" for persisted records)
multipart: Boolean Override multipart encoding. Defaults to auto-detection (true when any field is :file). Pass true or false to force.

Need more control? Override the partial by creating app/views/layered/ui/managed_resource/_form.html.erb in your host app, or write the form directly with form_with and reuse layered_ui/shared/form_errors, layered_ui/shared/label, and layered_ui/shared/field_error.

Field options

Field options
Option Type Description
attribute: Symbol Model attribute name (required)
as: Symbol Field type (:string, :text, :email, :password, :number, :tel, :url, :search, :date, :datetime, :time, :month, :week, :color, :range, :file, :select, :checkbox, :hidden). Inferred from column type when omitted.
label: String Custom label text (defaults to humanised attribute name)
required: Boolean Marks field as required with a visual indicator (defaults to false)
hint: String Help text displayed below the input
placeholder: String Placeholder text for the input
collection: Array / Proc Options for :select fields. Array of [label, value] pairs or a callable.
prompt: String Prompt text for :select fields (e.g. "Choose an author"). Shown as the first option; only selectable when no value is set. Setting prompt: suppresses the default blank option.
include_blank: Boolean / String For :select fields. Defaults to true. Pass a string to use it as the blank option's label, or false to omit the blank option entirely.