Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Form Descriptor

One YAML file describes one resource: the table, where it mounts, who may reach it, and its list and form. This page covers the resource keys and the form half; the list half is on List Descriptor.

# descriptors/posts.yaml
entity: posts
path: /posts
title: Posts
permission: acme.blog.manage_posts
list:
  creatable: true
  deletable: true
  order_by: created_at
  order_dir: desc
  columns:
    title:      { searchable: true }
    status:     { type: status_pill, width: 10% }
    created_at: { type: datetime, align: right }
form:
  title: Post
  fields:
    title:  { span: 2/3, rules: [required, { max_length: 120 }] }
    status: { span: 1/3, type: select, options: [{ value: draft, label: Draft }] }
    body:   { type: textarea, rows: 8 }

Register it

registry.add_resource(laterite_admin::resource!("descriptors/posts.yaml"));

The path is relative to your Cargo.toml. The file is read at compile time: a missing or malformed one fails the build.

KeyDescription
entityThe table both screens read and write. Required.
pathWhere the resource mounts, under your module’s namespace. Required.
titleThe navigation label, and the default title of both screens. Required.
permissionThe permission gating it. Required once a form is present.
id_fieldThe primary key column. Default id.
listThe list screen. Required.
formThe create and edit screen. Optional.
importtrue, or { permission: ... }, to let records be imported into the resource. Needs form. See Bulk Import. Optional.
relationsRelated records, keyed by name, each a section on the edit page. See Related Records. Needs form. Optional.
sourceKeeps the records as files in a folder of the project. See Records in Files. Needs form. Optional.

entity, path and id_field reach both screens.

Name a field

Fields are a map. The key is the name, and they render in the order written:

fields:
  title: {}
  published_at: { type: date }

A missing label is made from the key: published_at becomes “Published at”. It is still translatable, and lat i18n extract finds it.

Set a field’s keys

An entry carries its own keys and its type’s, side by side:

fields:
  body:   { type: textarea, rows: 8, placeholder: Write here, span: 1/2 }
  price:  { input: number, min: 0, step: 0.01, help: In the shop's currency }
  status: { type: select, options: [{ value: draft, label: Draft }], hidden: true }
KeyDefaultDescription
labelfrom the keyThe label shown.
typetextThe field type. Every type and its keys: Form Fields.
rulesnoneValidation rules run on submit. See Validation.
helpnoneA line beneath the control.
span, breakfull, falseThe field’s place on the row. See below.
enterthe form’ssubmit, off, next.
preset, triggernoneFollows or watches another field. See Field Dependencies.
formatnoneThe shapes accepted, the one stored, and how a link column shows it. See Field Formats.
hiddenfalseSubmitted but never shown: a plain hidden input carrying its value.
transientfalseShown and submitted with no column behind it: not loaded, not written by the built-in writer; the form’s persist handler reads it (a password field).
translatablefalseMarks the value as translatable content, for a translation plugin to manage.
contextevery pageThe pages the field is on: create, update, preview, one or a list. See below.
defaultnoneWhat a new record’s form opens with: text, a number, true or false, or a list for a field holding several.
default_fromnoneAnother field whose default this one opens with.
help_abovenoneText above the control, under the label.
css_classnoneClasses added to the field’s wrapper.
container_attributesnoneAttributes added to the field’s wrapper, as a map. An event handler, class, style, id and data-lat-* are refused.
read_onlyfalseShown with its value, not editable. A save keeps what is stored; a new record takes the field’s default.
disabledfalseShown, not editable, and left out of every save.
permissionnoneA permission the user must hold to see the field or write it. An editable list cell of the field follows the same rule.
depends_onnoneA field, or a list of fields: when one changes, this field is drawn again. See Field Dependencies.

A key the type does not read is refused, naming the entry and the keys it accepts.

Put fields on tabs

form:
  fields:
    title: {}
  tabs:
    default_tab: Details
    active_tab: Pricing
    icons: { Details: file-text, Pricing: tag }
    fields:
      body:  { type: textarea }
      price: { tab: Pricing, input: number }
      stock: { tab: Pricing, input: number }
  secondary_tabs:
    fields:
      notes: { tab: Notes, type: textarea }
PartHolds
fieldsThe fields above the tabs. One naming a tab joins the tabs.
tabs.fieldsThe fields on the tabs, each on the tab it names.
secondary_tabs.fieldsA second set of tabs, under the first.
Key of tabs and secondary_tabsDefaultDescription
default_tabMiscThe tab for a field in the set that names none.
active_tabthe firstThe tab open on load: its label, or its place from 1.
iconsnoneAn icon for a tab, by the tab’s label.
linkabletrueA tab opens by the address’s fragment, and writes it.
stretchfalseThe panes fill the height left on the page.
css_classnoneA class on the set.
pane_css_classnoneA class on every pane, or a map of tab label to class.
identifiersfrom the labelAn HTML id for a tab, by its label.

Tabs show in the order their fields first name them. A refused save opens the first tab holding a refused field.

lat.get(document.querySelector('[data-lat-widget="formtabs"]')).open('tab-pricing');
lat.on('formtabs:opened', function (detail) { console.log(detail.tab); });
FormField::text("price", t!("Price")).tab(t!("Pricing"));

Keep a field to some pages

fields:
  code:  {}
  token: { context: create }
  notes: { type: textarea, context: [update, preview] }
  status@create: { type: select, options: [{ value: draft, label: Draft }] }
  status@update: { type: select, options: [{ value: draft, label: Draft }, { value: live, label: Live }] }
FormMeaning
context: updateOn that page alone.
context: [create, update]On each page listed.
name@updateThe same, written on the key. One column may have an entry a page.
PageRoute
create{path}/new
update{path}/{id}/edit
preview{path}/{id}/preview

A field off a page is not shown, checked or saved there. An import and an ability write through every field.

FormField::text("token", t!("Token")).context([FormContext::Create]);

Set the form’s keys

form:
  enter: off
  confirm_leave: false
  focus: off
  timestamps: true
  footprints: true
  slugs: { slug: title }
  encrypt: [api_key]
  actions:
    - { label: Reset to default, action: "acme/templates/{id}/reset", confirm: Reset this template? }
    - { label: Download PDF, action: "acme/templates/{id}/pdf", method: get }
  fields:
    title: { rules: [{ max_length: 80 }], counter: true }
    body:  { type: textarea, rows: 8, grow: false }
    token: { type: password, reveal: false }
KeyOnValuesDefault
titleThe formtextThe resource’s title
enterThe formsubmit, offsubmit: Enter submits; Cmd/Ctrl+Enter submits from a textarea as well
confirm_leaveThe formtrue, falsetrue: leaving with changes unsaved asks first
focusThe formauto, offauto: a new record opens with the cursor in its first field, a refused save in the first field it refused
timestampsThe formtrue, falsefalse; true stamps created_at and updated_at on write
footprintsThe formtrue, falsefalse; true stamps created_user_id and updated_user_id. See Model listeners
slugsThe forma map of slug column to a source column or a listNone. See Model listeners
encryptThe forma list of columnsNone. See Model listeners
relatedThe forma map of one-to-one recordsNone. See Related Records
relatedA fielda name under the form’s relatedNone: the field is stored on that record
persistThe forma persister nameThe built-in insert and update. See Persisters
actionsThe forma list of label, action (a path under the admin root, {id} for the record), method (post or get), confirm, permission (hides the button without the grant)None. Shown beside Save on an existing record; post submits the form to action, get links to it
after_saveThe formcreate, update, close (paths under the admin root, {id} for the record), created, updated (text)Save returns to the list and says “Saved.”. create and update send Save elsewhere, and that page then offers “Save and close”, which goes to close or the list
panelsThe forma list of src, title, permission, as a preview’s panelsNone. Drawn under an existing record’s form, each fetched from src once the page is open
linked_recordThe formmorph (the name n of the columns n_type and n_id), label, emptyNone. Links the owning record above an edit form, named by its resource’s title and its list’s first column; empty (“Not linked”) shows when the columns are empty
enterA fieldsubmit, off, nextThe form’s

A max_length rule also sets the control’s maxlength.

Preview a record

# descriptors/orders.yaml
preview:
  title: Order
  assets: [chart.css]
  panels:
    - { title: Payments, src: "acme/orders/{id}/payments", permission: acme.payments }
list:
  row_click: preview

preview: true takes every default.

preview:
  fields:
    code:   { label: Reference }
    total:  { input: number }
  relations: false
KeyDefaultDescription
titlethe form’sThe page’s title.
assetsnoneAsset keys the panels need on the page.
panelsnoneParts drawn under the values, in order.
fieldsthe form’s on previewThe preview’s own fields, written as the form’s are.
relationstrueThe record’s related lists show under the values, to read.
Panel keyDescription
srcA path under the admin root answering an HTML fragment, {id} for the record’s id. Fetched once the page is open.
titleThe heading over the panel.
permissionA user without it does not get the panel.
RouteShows
{path}/{id}/previewThe form’s fields with their values, none editable, an Edit button, then the panels.

A previewed resource’s list shows a Preview link on each row, before Edit.

Lay out the fields

Fields lay out on a twelve-column row and wrap as it fills.

fields:
  title:  { span: 2/3 }
  status: { span: 1/3 }
  body:   { break: true }
KeyDescription
spanThe share of the row: full (default), 1/2, 1/3, 2/3, 1/4, 3/4, or 1 to 12 columns. left, right, auto and half are halves; third and quarter as named.
breaktrue starts a new row.

Under 1100px a quarter becomes a half; under 768px every field takes the row.

Catch a bad key

A key the framework does not know is refused with its line:

error: line 11 column 19: unknown field `searchabel`, expected one of
label, type, sortable, invisible, width, align, permission, searchable

Write only the keys you set; everything else takes its default.

lat doctor parses every descriptors/**/*.yaml in the application and its plugins and names a typo without a build.

Read a descriptor at run time

resource! embeds a file. For YAML you loaded yourself, call the function behind it:

let resource = laterite_admin::descriptor::from_yaml(&yaml, "posts.yaml")?;