Forms
A resource with create and edit pages declares a record form, the typed struct a submission
parses into. The record form also gives each field its control, and ResourceDef::form arranges
them. A resource that wants one control per field, in declaration order, declares no form at all.
The record form
#![allow(unused)]
fn main() {
#[derive(Debug, Clone, Copy, PartialEq, Eq, toasty::Embed, tablo::Options)]
pub enum Role {
#[option(icon = tablo::ui::icons::USER)]
Admin,
#[option(icon = tablo::ui::icons::USERS)]
Member,
}
#[derive(Debug, Clone, tablo::RecordForm)]
#[form(model = User)]
pub struct UserForm {
pub name: String,
pub email: String,
#[form(options, blank = Role::Member)]
pub role: Role,
#[form(blank = 0)]
pub age: i64,
}
}
Each field names a model field and has that field’s type, so renaming or retyping a column breaks
the build. A field is either a scalar — String, a typed value, an
Options enum, or an Option of one — bound to the one key its control posts, an
embedded value marked #[form(embed)], or a repeater marked
#[form(repeat)].
Leave out the columns the form does not write: the tenant column of a tenant-owned resource, which
the framework sets on create, and columns with a Toasty #[default(..)] or #[auto].
Required and blank values. The record form alone decides which fields are required. A field’s
blank answer is what it stores when its control is submitted empty, such as None for an
Option. A field with no blank answer is required: the panel renders its control required, and an
empty submission is refused inline. #[form(required)] takes the blank answer from an Option, so
a nullable column such as a product’s SKU is still filled through the form. RecordForm lists each
type’s blank answer and every #[form(..)] key.
The resource names the struct as its Form and, to arrange the controls, places them from the
derive’s controls() in a Schema<UserForm>:
#![allow(unused)]
fn main() {
impl Resource for UserResource {
type Model = User;
type Form = UserForm;
fn declare() -> ResourceDef<Self> {
let c = UserForm::controls();
ResourceDef::new()
.form(Schema::new(Section::new("Profile").schema((
c.name.placeholder("Ada Lovelace"),
c.email.email(),
c.role,
c.age,
))))
// .table(..), .policy(..) …
}
fn validate_record(_cx: &Cx, form: &UserForm) -> FieldErrors<UserForm> {
let mut errors = FieldErrors::<UserForm>::new();
if form.age < 0 {
errors.add(|f| f.age, "Age must be zero or more");
}
errors
}
}
}
A Schema<UserForm> takes only UserForm’s controls: a control of another form, or a field built
with Field::text, does not compile there. A control the schema does not place follows the ones
it does, in declaration order, so a form that adjusts one control places only that one:
.form(Schema::new(c.email.email())). An unplaced control renders on its own after the schema’s
last block, outside every section; a form arranged in sections places every control. Any control
takes an app’s own input with .custom(control): see Custom controls.
A resource with a record form serves a create page, whoever its policy lets in, so mounting the
panel refuses it unless every non-nullable column is filled by the form, by Toasty, by the tenant
stamp, or by an overridden create_record whose def names it with create_column. A resource
whose records arrive another way declares .without_page(ResourcePage::Create): it serves the edit
page, no create page, and skips the check. A resource whose create page should also take a CSV file declares
.importable().
A form from the model
When the record form is the model’s fields a person fills, derive tablo::ModelForm on the model
and it writes the struct, named after the model:
#![allow(unused)]
fn main() {
#[derive(Debug, Clone, toasty::Model, tablo::ModelForm)]
pub struct Note {
#[key]
#[auto]
pub id: uuid::Uuid,
#[index]
pub tenant_id: TenantId,
pub title: String,
#[form(optional)]
pub body: String,
#[default(jiff::Timestamp::now())]
pub created_at: jiff::Timestamp,
#[index]
#[form(relationship = AuthorResource)]
pub author_id: uuid::Uuid,
#[belongs_to(key = author_id, references = id)]
pub author: toasty::Deferred<Author>,
}
}
NoteForm holds title, body and author_id, with NoteFormField and NoteFormControls
beside it. The derive leaves out what no person fills, such as the key and the tenant column, and
author_id is in only because #[form(relationship = AuthorResource)] opts the foreign key in:
without it, a create would leave the column unset, which mounting refuses.
ModelForm lists what it leaves out and how to opt a field in or
out.
Declare a #[derive(RecordForm)] struct instead when a form field is not a model column, such as
a many-to-many field’s list of keys, or when the model should not name a
resource. Move the field keys onto it and drop ModelForm from the model, or give one of the two
another name: both are NoteForm by default.
Controls
The derive picks each field’s control from its type and its #[form(..)] key, as
RecordForm lists: a bool is a toggle, #[form(options)] a
choice, and a field with no key a text field. controls() hands each one over ready for its
modifiers, so an override arranges rather than rebinds:
#![allow(unused)]
fn main() {
pub fn account_layout() -> Schema<UserForm> {
let c = UserForm::controls();
Schema::new((
Section::new("Account").schema((c.email.email(), c.role)),
Grid::new(2).schema((c.name, c.age)),
))
}
}
The role control already offers Role’s options: #[form(options)] chose a choice over the
list of the field’s type. A closed set of values is a unit enum deriving toasty::Embed, which
Toasty stores in one column, and tablo::Options, which gives the form, the filter and the column
one list of options:
#![allow(unused)]
fn main() {
pub fn role_fields() {
Field::choice(lens!(User.role)).options(Role::options());
SelectFilter::of(lens!(User.role));
}
}
Each variant posts its snake_case name and reads as that name in sentence case, unless a
catalog translates it;
Options has the attributes that override either. A column, the
detail page and a group header read a variant’s label, and a query compares the variant itself: User::fields().role().eq(Role::Admin). An Option<Role>
field works the same way, reading an empty submission as None. An enum field without
#[form(options)] gets a text field, which accepts only an option’s value. An enum leaf of an
embedded value is such a text field, and its column shows the value.
Toasty stores the variant itself, under its snake_case name unless #[column(variant = "..")]
renames it; the option’s value only spells it in forms and URLs. Moving a String column to an
enum is therefore a schema change: rows whose text is not Toasty’s name for a variant no longer
load.
A String field takes the same list with #[form(options = T)]. .options takes
Vec<(String, String)> (an Options enum’s list), Vec<String>, or [&str; N]
(["admin", "member"]).
A Vec of an Options enum with #[form(options)], or a Vec<String> with
#[form(options = T)], is a multiple choice: a checkbox per option, and Toasty stores the
chosen ones in one list column.
#![allow(unused)]
fn main() {
#[derive(Debug, Clone, Copy, PartialEq, Eq, toasty::Embed, tablo::Options)]
pub enum Topic {
Releases,
Security,
Events,
}
#[derive(Debug, Clone, toasty::Model)]
pub struct Subscriber {
#[key]
#[auto]
pub id: uuid::Uuid,
pub email: String,
// A list column: one value per chosen option.
pub topics: Vec<Topic>,
}
#[derive(Debug, Clone, tablo::RecordForm)]
#[form(model = Subscriber)]
pub struct SubscriberForm {
pub email: String,
#[form(options)]
pub topics: Vec<Topic>,
}
pub fn subscriber_layout() -> Schema<SubscriberForm> {
let c = SubscriberForm::controls();
Schema::new((c.email, c.topics.min_items(1).buttons()))
}
}
No option checked stores the empty list, so .min_items(1) is how the field requires a choice;
.max_items(n) caps it, and .buttons() renders a compact list of toggles.
RecordForm has the rules.
A Vec<String> marked #[form(tags)] takes what the user types instead: a checkbox per value
it holds and per suggestion, then a text input adding values separated by commas.
#![allow(unused)]
fn main() {
#[derive(Debug, Clone, toasty::Model)]
pub struct Snippet {
#[key]
#[auto]
pub id: uuid::Uuid,
pub title: String,
pub labels: Vec<String>,
}
#[derive(Debug, Clone, tablo::RecordForm)]
#[form(model = Snippet)]
pub struct SnippetForm {
pub title: String,
// Any label the user types, with the topics as suggestions.
#[form(tags, options = Topic)]
pub labels: Vec<String>,
}
}
ChoiceField::tags
has the rules.
Layout blocks arrange fields: Section::new(title) is a titled card, Group::new() an untitled
container, and Grid::new(cols) a grid of 1 to 12 columns. A schema or block takes a tuple of at
most twelve children; nest a Group for more.
A section can say what its fields are for and where they sit. .description adds a line under the
title and .icon an icon before it; .aside() puts both beside the fields on a wide screen, so a
long form reads as a list of topics. .collapsible() lets the title fold the fields away, and
.collapsed() opens the page with them folded, for fields the user rarely changes:
#![allow(unused)]
fn main() {
pub fn account_sections() -> Schema<UserForm> {
let c = UserForm::controls();
Schema::new((
Section::new("Account")
.description("How the user signs in.")
.icon(tablo::ui::icons::USERS)
.aside()
.schema((c.email.email(), c.role)),
Section::new("Profile")
.collapsed()
.schema(Grid::new(2).schema((c.name, c.age))),
))
}
}
A folded section’s fields still post, and a section holding a field error renders open.
Section has the full rules.
A grid stacks its children in one column until it is wide enough for its columns, so a form reads
the same on a phone. A block or a field takes .span(n) to cover n of its grid’s columns, and
.span_full() to cover them all. A three-column grid whose first section spans two puts the
fields the user edits most in a main column and the rest in a sidebar beside it:
#![allow(unused)]
fn main() {
pub fn account_sidebar() -> Schema<UserForm> {
let c = UserForm::controls();
Schema::new(
Grid::new(3).schema((
Section::new("Profile")
.span(2)
.schema(Grid::new(2).schema((c.name.span_full(), c.age))),
Section::new("Account").schema((c.email.email(), c.role)),
)),
)
}
}
Grid has the widths at which each grid
lays out its columns.
Tabs::new shows one Tab at a time. Each tab’s fields still render and post while another
tab shows, so splitting a long form into tabs changes what the user sees, not what the form
submits. A tab’s trigger can lead with an icon and close with a badge, which a color dots:
#![allow(unused)]
fn main() {
pub fn account_tabs() -> Schema<UserForm> {
let c = UserForm::controls();
Schema::new(Tabs::new([
Tab::new("Account")
.icon(tablo::ui::icons::MAIL)
.schema((c.email.email(), c.role)),
Tab::new("Profile")
.icon(tablo::ui::icons::USER)
.badge("New")
.badge_color(tablo::Color::Info)
.schema(Grid::new(2).schema((c.name, c.age))),
]))
}
}
A refused submission reopens on the first tab holding an error, and the browser opens the tab of a
control it refuses itself. Tabs has the
full rules.
Wizard::new walks the user through its Steps in order, with a button to the next step and one
back. The next step opens once the browser’s own checks pass on the current one, and Enter in a
field moves forward rather than saving. As with tabs, every step’s fields render and post, and the
form’s own buttons save from any step:
#![allow(unused)]
fn main() {
pub fn account_wizard() -> Schema<UserForm> {
let c = UserForm::controls();
Schema::new(Wizard::new([
Step::new("Account").schema((c.email.email(), c.role)),
Step::new("Profile").schema(Grid::new(2).schema((c.name, c.age))),
]))
}
}
A refused submission reopens on the first step holding an error.
Wizard has the full rules.
Fields are what controls() hands over, and what a page’s or an action’s schema builds from
a Toasty field lens:
| Constructor | Column | Control | Builder |
|---|---|---|---|
Field::text(lens) | String, a typed value, or an Option of one | <input>, or <textarea> with .multiline(rows) | TextField |
Field::choice(lens) | any | <select> over static options or a relationship | ChoiceField |
Field::rich_text(lens) | String holding HTML | an editor with a toolbar: see Rich text | RichTextField |
Field::file(lens) | String holding the file’s path | file input: see File uploads | FileField |
Field::files(lens) | Vec<String> holding the files’ paths | file list: see Several files per field | FileField |
Field::toggle(lens) | bool | checkbox | CustomField |
Field::custom(lens, control) | String, a typed value, or an Option of one | your own Control: see Custom controls | CustomField |
Every field takes .label(..), which defaults to the column name in sentence case. Whether a
control is required is not the control’s to say: on a create or edit page the panel renders it
required when its record-form field has no blank answer (a file input only while nothing is
stored). Outside a panel no record form applies, and every control renders optional. Each constructor returns its
control’s builder, which offers only that control’s modifiers, so a modifier on the wrong control
does not compile:
TextField, ChoiceField,
RichTextField and CustomField
list each builder’s modifiers, such as .url(), .relationship::<R>(), .switch() or
.accepted(). A multiple choice is required through
ChoiceField::min_items,
since its blank answer is the empty list. A choice needs something to offer, so mounting refuses a
resource form’s or an action input’s choice with neither options nor a relationship.
Custom controls
A Control, from tablo::extend, renders the input of a custom field: a record form’s text control
turned custom with .custom(control), or a Field::custom field. .custom (like .choice()) replaces the control
with its modifiers, so email, unique and options set before it no longer apply. The field keeps everything fields share —
the key, the label, the required marker, the error slot and the chrome around the input — and the
control renders only the input, from a ControlInput carrying the key, the current value and the
validation state:
#![allow(unused)]
fn main() {
struct Color;
impl Control for Color {
fn render<'a>(&self, cx: &'a Cx, input: ControlInput) -> BoxView<'a> {
let attrs = input.attributes(cx); // id, name, value, required, aria-*
view! { <input type="color" (attrs)> }.boxed()
}
}
// A record form's text control takes it with `.custom`; a page's field with `Field::custom`.
pub fn accent_control() -> CustomField<ThemeForm> {
ThemeForm::controls().accent.custom(Color)
}
pub fn accent_field() -> CustomField {
Field::custom(lens!(Theme.accent), Color)
}
}
A control renders only the form; the detail page shows the value through a
column. The submission is read like any other field’s:
the value posted under the field’s key, the last one when it is posted twice, parsed by the record
form. Field::toggle is built this way: Toggle renders a hidden false before the checkbox
under the same name, so an unchecked box submits false rather than nothing, and a bool
record-form field reads an empty submission as false.
Typed values
A text control binds more than strings. Over a typed column it renders the stored value in the
input its type names, and the record form parses the submission back through the type. A value the
type refuses is an inline error naming it: `twelve` is not a valid whole number.
Integers and floats get a number input, the jiff civil types and Timestamp a date or time
input; TypedValue lists each type’s input.
An amount that must stay exact, such as a price, is a decimal column. The tablo features
rust_decimal and bigdecimal bind rust_decimal::Decimal and bigdecimal::BigDecimal, and turn
on Toasty’s feature of the same name, which stores them:
[dependencies]
tablo = { version = "0.11.1", features = ["sqlite", "rust_decimal"] }
rust_decimal = "1"
A decimal gets a number input and keeps the scale it was typed with, so 12.50 is stored as
12.50. A spelling the type cannot hold exactly, such as an exponent or more decimals than
Decimal carries, is an inline error rather than a rounded value.
A decimal column is not sortable by default: SQLite, MySQL and Turso store a Decimal as text, and
every driver stores a BigDecimal as text, so the database would order 10.00 before 9.50. The
PanelModel derive leaves it unsortable; declare .sortable() only on PostgreSQL with
Decimal.
Implement tablo::extend::TypedValue to bind your own
type: it names the type in errors, picks the input, and parses and spells the value, with a default
for each item but the name.
Time zones
A datetime-local input posts a wall-clock time without a zone, so the panel places it in the
request’s zone: tablo::time_zone(cx) answers the signed-in user’s PanelUser::time_zone, else
the panel’s Panel::time_zone, else UTC.
#![allow(unused)]
fn main() {
pub fn paris_panel() -> Panel {
let paris = jiff::tz::TimeZone::get("Europe/Paris").expect("the zone database names it");
Panel::new("admin").time_zone(paris)
}
}
A timestamp’s control therefore shows the stored instant as the user’s own wall-clock time, and a
DateColumn or DateFilter reads the user’s own day: 14:00 entered in Paris on 20 October
stores 12:00:00Z. The stored value stays the instant, which the export spells in UTC.
time_zone has the rules for skipped and repeated times, offsets, and
custom controls.
Length, bounds and patterns
A text field declares the rules a value keeps. Each renders as the HTML attribute of the same name, so the browser checks it before it submits, and the server checks it again; a refused value is an inline error, and nothing is written. A value the record already stores is not checked again, so a rule added over existing rows does not lock them.
#![allow(unused)]
fn main() {
pub fn checked_account_form() -> Schema<UserForm> {
let c = UserForm::controls();
Schema::new((
c.name.min_length(2).max_length(80),
c.email.email().pattern(r".+@example\.com"),
c.role,
c.age.min(18).max(130),
))
}
}
TextField has each rule’s details: what a length counts, which
types take a bound, and the regular-expression syntax a pattern shares between the server and
the browser. A rule that cannot work, such as a minimum above its maximum, fails the mount.
A text field also compares with another field of its form: .gt(&other), .gte, .lt and .lte
order two numbers, dates or times, and .different(&other) refuses an equal value, as
ends_on.gte(&starts_on) keeps a stay from ending before it starts. Only the server checks them,
in the same round as the other rules; TextField::gt
has the rules. A rule these do not express belongs in Resource::validate_record.
Email, URL and uniqueness
.email() refuses what is not an address, and .url() what is not an http or https URL: the
browser’s own check accepts a javascript: URL, which a link to the stored value would run.
A text field over a column with a unique index checks uniqueness before the write, and reports a
taken value inline; .unique() states it explicitly. The database index stays the final check,
so a unique field over a non-nullable column must be required, or every empty submission would
store the same value. TextField::unique says what a
concurrent write answers on each database.
Relationships
A choice over a foreign key loads its options from the related resource. Mark the record-form field
#[form(relationship = AuthorResource)]; the form arranges and labels its control like any other:
#![allow(unused)]
fn main() {
#[derive(Debug, Clone, tablo::RecordForm)]
#[form(model = Post)]
pub struct PostAuthorForm {
// The source whose scoped query loads the options; each is labelled by its `record_title`.
#[form(relationship = AuthorResource)]
pub author_id: uuid::Uuid,
}
pub fn author_control() -> ChoiceField<PostAuthorForm> {
PostAuthorForm::controls()
.author_id
.searchable()
.label("Author")
}
}
Each option’s value is the related record’s primary key, and its text the record’s title: the
column the related resource’s record_title names, else its label and the key. Typing into a
searchable choice matches that column and the related table’s searchable columns. A source that is
not a resource implements OptionSource, including its label. .relationship_labelled::<R>(|cx, a| ..)
labels one field’s options otherwise, to tell apart records that share a title.
Options follow the related resource’s tenant scope and policy, and a submitted key must be one of
them, checked again inside the write’s transaction, so a hand-crafted POST cannot point at another
tenant’s row. Past 200 options a .searchable() choice turns into type-to-search.
ChoiceField::relationship has the rules.
Many-to-many fields
A many-to-many relation stores one row per pair in a join model with a belongs_to to each
side. The model reaches the other side through it with a #[has_many(via = ..)] field:
#![allow(unused)]
fn main() {
#[derive(Debug, Clone, toasty::Model, tablo::PanelModel)]
pub struct Article {
#[key]
#[auto]
pub id: uuid::Uuid,
pub title: String,
#[has_many]
pub taggings: toasty::Deferred<Vec<Tagging>>,
// The tags the join rows reach.
#[has_many(via = taggings.tag)]
pub tags: toasty::Deferred<Vec<Tag>>,
}
#[derive(Debug, Clone, toasty::Model, tablo::PanelModel)]
pub struct Tag {
#[key]
#[auto]
pub id: uuid::Uuid,
pub name: String,
#[has_many]
pub taggings: toasty::Deferred<Vec<Tagging>>,
#[has_many(via = taggings.article)]
pub articles: toasty::Deferred<Vec<Article>>,
}
/// The join model: one row per article and tag.
#[derive(Debug, Clone, toasty::Model)]
#[key(article_id, tag_id)]
pub struct Tagging {
#[index]
pub article_id: uuid::Uuid,
#[belongs_to(key = article_id, references = id)]
pub article: toasty::Deferred<Article>,
#[index]
pub tag_id: uuid::Uuid,
#[belongs_to(key = tag_id, references = id)]
pub tag: toasty::Deferred<Tag>,
}
}
The record form names that field, typed as a Vec of the related records’ keys, and marks it
with the related resource:
#![allow(unused)]
fn main() {
#[derive(Debug, Clone, tablo::RecordForm)]
#[form(model = Article)]
pub struct ArticleForm {
pub title: String,
// Named like the `via` field, holding the keys of the tags it reaches.
#[form(relationship = TagResource)]
pub tags: Vec<uuid::Uuid>,
}
pub fn article_layout() -> Schema<ArticleForm> {
let c = ArticleForm::controls();
Schema::new((c.title, c.tags.searchable()))
}
}
The control is a checkbox per record the related resource offers. A create links the new record
to each chosen one, one join row each, and an edit adds and deletes rows to match, unlinking only
the records its form could offer. An overridden create_record or update_record links only by
delegating to write_create or write_update. Making the two keys the join model’s primary key,
#[key(article_id, tag_id)] as above, has the database store each pair once.
RecordForm has the rules, the option cap and
what the mount refuses.
Dependent choices
.depends_on(&field, column) narrows a relationship choice to the related records whose column
equals the value another field of the same form posts: the cities of the chosen country.
#![allow(unused)]
fn main() {
#[derive(Debug, Clone, tablo::RecordForm)]
#[form(model = Address)]
pub struct AddressForm {
#[form(relationship = CountryResource)]
pub country_id: uuid::Uuid,
#[form(relationship = CityResource)]
pub city_id: uuid::Uuid,
}
pub fn address_layout() -> Schema<AddressForm> {
let c = AddressForm::controls();
// The cities whose `country_id` is the chosen country.
let city = c
.city_id
.depends_on(&c.country_id, City::fields().country_id());
Schema::new((c.country_id, city))
}
}
When the parent changes, the browser fetches the new options, and a submission must name a record
of the parent value it posts. ChoiceField::depends_on
has the rules and what the mount refuses.
Filtered choices
.options_where(|cx| predicate) narrows a relationship choice to the related records a predicate
selects, whatever the form posts: only the categories without a parent may be one.
c.parent_id
.options_where(|_cx| Category::fields().parent_id().is_none())
The predicate is built for each request, so it may read the user or the tenant, and it narrows a
dependent choice’s records too. The server checks a submitted key against it on submit and again
in the write’s transaction. ChoiceField::options_where
has the rules and what the mount refuses.
Creating a related record
.creatable() lets the user create the related record without leaving the form: a link beside
the select opens the related resource’s create page in a dialog, and the record it saves becomes
the choice. The form keeps what was typed in it.
#![allow(unused)]
fn main() {
pub fn creatable_author_control() -> ChoiceField<PostAuthorForm> {
// A "New" link beside the select opens the authors' create page in a dialog.
PostAuthorForm::controls()
.author_id
.searchable()
.creatable()
}
}
The dialog runs the related resource’s own create page; ChoiceField::creatable
has the rules and what the mount refuses.
Help, disabled controls and defaults
Every control but a repeater takes .help(text), rendered under it, and .disabled() or
.disabled_on_edit(). A text, choice or custom control also takes .default(value), the value the
create form starts with: any form scalar, or a &str in the form spelling. A multiple choice’s
default is an array or Vec of them: .multiple().default(["email", "sms"]).
#![allow(unused)]
fn main() {
pub fn account_form() -> Schema<UserForm> {
let c = UserForm::controls();
Schema::new((
c.name.help("As it appears on invoices."),
c.email
.disabled_on_edit()
.help("The sign-in address: it cannot change once the account exists."),
c.role.default(Role::Member).disabled(),
c.age.default(18_i64),
))
}
}
A disabled control posts nothing, so an edit keeps the stored value and a create stores the
default, else the blank answer; .disabled() on a field with neither cannot mount, since no create
could fill it. A default seeds the create form, where the blank answer is what an empty submission
stores. Each modifier’s page has its rules, such as
default.
A record form’s field also takes .hidden_on_create() or .hidden_on_edit(), which renders nothing
for it on that form. Here an invite sets the address once and the role only once the account exists:
#![allow(unused)]
fn main() {
pub fn invite_form() -> Schema<UserForm> {
let c = UserForm::controls();
Schema::new((
c.name,
c.email.hidden_on_edit(),
c.role.hidden_on_create(),
c.age,
))
}
}
The server reads a hidden field as it reads a disabled one: a create stores its default, else its blank answer, and an edit keeps the stored value.
Conditional fields
.visible_when(&field, values) shows a field only while another field of the same form posts one
of values: an option’s value, or true or false for a toggle. A Section, Group or Grid
takes it too, for every field it holds:
#![allow(unused)]
fn main() {
#[derive(tablo::RecordForm)]
#[form(model = User)]
pub struct AccessForm {
#[form(options, blank = Role::Member)]
pub role: Role,
pub sso_managed: bool,
}
pub fn access_layout() -> Schema<AccessForm> {
let c = AccessForm::controls();
let sso = c.sso_managed.visible_when(&c.role, ["admin"]);
Schema::new(Section::new("Access").schema((c.role, sso)))
}
}
visible_when borrows the watched field, so build the condition before the schema takes it.
The browser shows and hides the field as the watched one changes, with no request, and the panel
reads a submission the same way, dropping a hidden field’s key. A conditional field therefore needs
a blank answer. visible_when has the rules and
what the mount refuses.
.disabled_when(&field, values) keeps the control on the page but disables it while the watched
field posts one of values: a member’s account cannot be SSO-managed.
#![allow(unused)]
fn main() {
pub fn locked_access_layout() -> Schema<AccessForm> {
let c = AccessForm::controls();
let sso = c.sso_managed.disabled_when(&c.role, ["member"]);
Schema::new((c.role, sso))
}
}
While the condition holds, the server reads the field as it reads a .disabled() one, so a create
stores its default and an edit keeps the stored value. The condition reads what the submission
posts, so it is no access check: field access is.
disabled_when has the rules and what the mount
refuses.
Submitting
Every error renders inline in one round, with nothing written: the parse, the controls’ rules,
relationships and uniqueness together, then validate_record once every field parses, keyed by
the record form, each error picking its field by name
(errors.add(|f| f.age, "Age must be zero or more")). A POST
carrying a key the form does not declare answers 400, so a client cannot write role or
tenant_id; an edit writes only the fields it posted; and a model with a #[version] field
refuses a save another save overtook. RecordForm has
the rules.
The create page has a second button, Create & create another, for entering several records in a row: it runs every check above, and a save lands back on the create form with the success flash, its fields at their defaults. From a relation table’s create link, the next form keeps the owner preselected.
The form page’s script saves on Ctrl+S (Cmd+S on macOS), and asks before a followed link, a
reload or a closed tab drops what the user typed; a refused save asks from the start, since its
values are the user’s. The browser’s back and forward buttons leave without asking. Without
JavaScript the form posts as it renders and the page leaves without asking.
To write more than the form holds, override create_record or update_record
(Resources) and use the builders: form.into_create() is the model’s
create builder with every form field set, and posted.into_update(&mut record) is the update
builder with one assignment per posted field, or None when nothing was posted.
Rich text
A rich text field edits HTML through a toolbar and stores it in a String or Option<String>
column. A record form’s text control becomes one with .rich_text(), and a panel whose field of
another type does refuses to mount; a page or an action input builds one with
Field::rich_text(lens) or Field::rich_text_input(name):
#![allow(unused)]
fn main() {
pub fn post_content_form() -> Schema<crate::resources::PostForm> {
let c = crate::resources::PostForm::controls();
Schema::new((
c.title,
c.body
.rich_text()
.toolbar(&[
&[Tool::Bold, Tool::Italic, Tool::Link],
&[Tool::Heading2, Tool::BulletList, Tool::OrderedList],
])
.placeholder("The full story…")
.max_length(20_000),
c.status,
))
}
}
The toolbar is the schema: the editor registers only the elements its tools produce, and the
server cleans every submission down to them, so a <script> or a javascript: link never reaches
the column. A text column moving to rich text needs its values rewritten through
tablo::rich_text::from_text first. RichTextField has the
rules.
RichTextColumn::new(lens) shows the value on a detail page as formatted HTML,
and in a table and an export as its text. It cleans the value again as it renders, keeping what any
Tool produces, since a seed or another app can write the column. A page of your own renders a
stored value through tablo::rich_text::clean, unescaped, and reads its text through
tablo::rich_text::text.
File uploads
A file field (#[form(file)] on a record-form String, or Field::file on a page) binds a
String column that stores the file’s path or URL, never its bytes; a file list binds a
Vec<String> of them (Several files per field). A form
with a file field is sent as multipart/form-data, with a 10 MiB body limit (larger answers 413)
that Panel::limits raises with Limits::body_bytes.
Filenames are reduced to a safe basename before anything sees them.
Where the bytes go is your app’s decision; a panel whose resource form declares a file field
refuses to mount without an Uploader. Panel::uploads_dir installs the stock one, a
DirUploader, and serves its directory:
#![allow(unused)]
fn main() {
pub fn uploads_panel(dir: PathBuf) -> Panel {
Panel::new("admin").uploads_dir("/uploads", dir)
}
}
It writes each file into the directory as {uuid}-{basename}, within the filesystem’s 255-byte
name limit, and stores its URL: /uploads/ and the name as one percent-encoded path segment. The
directory answers that URL only with a signature that expires, which the panel adds wherever it
links or shows a stored file; a page of your own adds it with url::file(cx, &path). Once a write
commits, it deletes the files the write let go: those of a deleted record, and the one an edit
replaced or cleared; a soft delete keeps them until the force delete. The upload of a form that
never saved, such as one refused for another field that the user then left, is deleted after
24 hours.
A file field takes any file until it declares .extensions(["png", "jpg"]) or .max_bytes(n).
The server refuses a file either rule refuses before the uploader sees it, and the extensions also
reach the browser’s file picker as accept. The extension is what is checked, not the content: a
served file’s type comes from its extension, so the extension decides how a browser treats it.
FileField::extensions
has the rules.
The field is one panel: a drop area to click or drop files on, then the stored file and the files
picked or dropped before the form saves them, each with a round button removing it. .image()
shows them as tiles of their images; it changes what the field shows, not what it takes, so pair
it with .extensions. Without JavaScript a click on the drop area still opens the browser’s file
picker.
Any other store implements Uploader. This one refuses a file whose first bytes are not a PNG’s
or a JPEG’s signature, which no field rule checks, then stores through the DirUploader that
Panel::uploads_dir_with hands it, so its files still answer only at signed URLs. It forwards
keep and delete too: the inner uploader deletes a file nothing kept after a day. A signature
is no proof the rest is an image: decode the file when that matters.
#![allow(unused)]
fn main() {
/// Refuses bytes that do not start as a PNG or JPEG image, then stores like `uploads_dir` does.
struct ImagesOnly(DirUploader);
impl Uploader for ImagesOnly {
async fn store(&self, cx: &Cx, filename: &str, bytes: &[u8]) -> Result<String, String> {
let png = bytes.starts_with(b"\x89PNG\r\n\x1a\n");
let jpeg = bytes.starts_with(&[0xff, 0xd8, 0xff]);
if !png && !jpeg {
return Err("the file is not a PNG or JPEG image".to_string());
}
self.0.store(cx, filename, bytes).await
}
async fn holds(&self, cx: &Cx, path: &str) -> bool {
self.0.holds(cx, path).await
}
async fn keep(&self, cx: &Cx, path: &str) -> Result<(), String> {
self.0.keep(cx, path).await
}
async fn delete(&self, cx: &Cx, path: &str) -> Result<(), String> {
self.0.delete(cx, path).await
}
}
pub fn images_panel(dir: PathBuf) -> Panel {
Panel::new("admin").uploads_dir_with("/uploads", dir, ImagesOnly)
}
}
store returns the value to save, and its error is shown to the user, so keep it free of paths.
The edit form keeps a stored file until the user removes it, and a form that re-renders with errors
reuses an upload only under the tag the panel signed for its field, where Uploader::holds vouches
for the path.
Uploader has the rules, and Security the headers
served files carry.
Several files per field
#[form(file)] on a Vec<String>, or Field::files on a page, makes a file list: the column
stores one path per file, in the order the form lists them. Its input takes several files at once,
which a save adds after the stored ones, and each stored file has its own remove checkbox, so the
list works without JavaScript. With it, each file also has Move earlier and Move later buttons
and can be dragged, by its grip on a touch screen, and the list saves in the order it shows. Files dropped on the input’s
zone join the ones already picked, and .image() shows every stored and picked file as a
thumbnail. Only the remove checkbox drops a stored file: a file another save added while the form
was open stays. The extensions and size apply to each file, and max_files bounds how many the
list holds once a save adds its files; choosing more than that stores none:
#![allow(unused)]
fn main() {
#[derive(Debug, Clone, toasty::Model)]
pub struct Album {
#[key]
#[auto]
pub id: uuid::Uuid,
pub title: String,
pub photos: Vec<String>,
}
#[derive(Debug, Clone, tablo::RecordForm)]
#[form(model = Album)]
pub struct AlbumForm {
pub title: String,
#[form(file)]
pub photos: Vec<String>,
}
pub fn album_form() -> Schema<AlbumForm> {
let c = AlbumForm::controls();
Schema::new((
c.title,
c.photos.image().extensions(["png", "jpg"]).max_files(12),
))
}
}
A refused save keeps the files it stored, as a single file field does, so the user drops one and
saves again. Every file still shares the one form body limit, so a list of large files needs a
raised Limits::body_bytes.
Field::files has the
rules.
Embedded values
A Toasty #[derive(Embed)] struct or enum is stored in its parent’s row as flattened columns
(seo_title, seo_description). Derive EmbeddedForm on it and the form binds the whole value:
#![allow(unused)]
fn main() {
pub struct Seo {
pub title: String,
#[form(multiline = 3)]
pub description: String,
}
// In the def's form: one call renders a control per field.
pub fn seo_schema() -> Section {
Section::new("SEO").schema(Seo::form(Post::fields().seo()))
}
// In the record form: the value is one field.
#[derive(Debug, Clone, tablo::RecordForm)]
#[form(model = Post)]
pub struct PostForm {
pub title: String,
#[form(embed)]
pub seo: Seo,
}
}
Every field of the value is a scalar, or a nested value marked #[form(embed)]; an enum renders
a choice of variant and the chosen variant’s fields. On the detail page,
Post::columns().seo shows each field under its own label.
EmbeddedForm has the rules: which fields are required,
which variant a submission reads, the attributes, and what a value cannot hold.
A resource’s form places an embedded value whole, as its record form’s control. A page’s schema
can bind a single embedded field on its own by its path: Field::text(lens!(Post.seo.title))
binds the flattened seo_title column, as soon as the field is built.
Repeaters
Toasty stores a #[document] list of embedded structs in one column. Derive FormInput
on the struct and mark the record-form field #[form(repeat)]: the form renders a row of the
item’s controls per item, which the user adds, removes and moves.
#![allow(unused)]
fn main() {
#[derive(Debug, Clone, toasty::Embed, tablo::FormInput)]
pub struct RecipeStep {
#[form(multiline = 2)]
pub instruction: String,
#[form(blank = 0)]
pub minutes: i64,
}
#[derive(Debug, Clone, toasty::Model, tablo::PanelModel)]
pub struct Recipe {
#[key]
#[auto]
pub id: uuid::Uuid,
pub title: String,
// Stored in one column.
#[document]
pub steps: Vec<RecipeStep>,
}
#[derive(Debug, Clone, tablo::RecordForm)]
#[form(model = Recipe)]
pub struct RecipeForm {
pub title: String,
#[form(repeat)]
pub steps: Vec<RecipeStep>,
}
pub fn recipe_form() -> Schema<RecipeForm> {
let c = RecipeForm::controls();
Schema::new((c.title, c.steps.label("Method").add_label("Add step")))
}
pub fn recipe_view() -> Detail<Recipe> {
Detail::new(Recipe::columns().steps.repeater().label("Method"))
}
}
An item’s fields are scalars, declared like an action input’s with the
FormInput attributes, and a field with no blank answer is
required in every row. Each row posts its item’s fields under its own prefix, such as steps.0.minutes, and an edit that
does not post the repeater keeps the stored rows. Adding, removing and moving rows needs
JavaScript. Field::repeater has the rules and the limits.