Create and edit records
form is the region that writes one row. It renders a control per field, validates the
submission, and writes it in one transaction.
Add a form
Section titled “Add a form”page member "Member" { route: /members/{member} new_route: new access: board
form of members { fields: first_name, last_name, email, fee_group, joined_on actions: save, delete success: "Member saved." }}One page serves both addresses. new_route: new puts new in place of {member}, so the
second address is /members/new. On /members/new the form is empty and a save inserts. On
/members/42 the form is filled and a save updates.
actions: decides which buttons exist. A delete renders as a separate confirmation that is two
deliberate clicks away, and it needs no JavaScript. To give the delete to fewer roles than the
save, add delete { access: board }; see Let only some roles delete.
What a save does
Section titled “What a save does”A save is a POST, then a redirect, then a GET. The message survives the redirect, so a reload never repeats a write.
The redirect goes back to the page the form was opened from. Open member 42 from page 2 of a
filtered list, save, and you are on page 2 of that list again. To send the save somewhere else,
write after_save: on the form.
A form opened from navigation, a bookmark, or a typed address has no origin to return to.
It uses its own row address after a save when one exists. A parameterless create page stays
on that page. Give it after_save: claims to return to a listing page named claims.
Before anything is written, the engine checks the submission against the column types and against every rule the table declares. If a check fails, the form renders again with the exact text the person typed and a message on each field that failed.
The form does not reformat the other forty fields while it rejects three. A person has to find their own mistake.
Fill in a value the user never types
Section titled “Fill in a value the user never types”set writes a column from the request rather than from a control. The column gets no control
at all, so a request cannot supply it.
page signup "Sign up" { route: /events/{event}/signup access: member
form of event_signups { fields: note actions: save
set event = :route.event set person = :user.person_id }}A hidden field can be posted by anyone. A set is not readable from the request at all, which
is the difference that matters.
Every set reads the same candidate row before any sets apply. A set cannot read a value
that another set just wrote, even if that declaration comes first. To copy a profile row’s
department, load that row with data and read it directly:
data me of employees where id = :user.employee_id
form of claims { fields: claimed_on, amount, description set employee = :me.id set department = :me.department after_save: claims}Use this on a parameterless create page. On a page with a row parameter, that parameter
binds the data row; use a separate edit page for the claim.
Refuse a save with a reason
Section titled “Refuse a save with a reason”deny when refuses the whole form when a condition holds.
form of event_signups { fields: note actions: save
set event = :route.event
deny when event.signups >= event.capacity "This event is fully booked."}The message replaces the form on a GET, so nobody fills in a form that cannot be saved. On a
POST the same rule is checked again inside the write’s own transaction. Two people who
press save at the same moment cannot both pass a capacity check. A deny when belongs to one
form. To hold every writer to a rule about other rows, write a table check. See
Refuse a booking that overlaps another.
Check a value against the row
Section titled “Check a value against the row”A table can declare rules that the engine evaluates against the row that a save produces.
table events "Events" { display: name
name text required "Name" starts_at datetime required "Starts" ends_at datetime "Ends" capacity int "Capacity" { min: 1 }
check ends_at >= starts_at "The end must not be before the start."
require capacity when kind = limited "A limited event needs a capacity."
unique(name, starts_at) "An event with this name already exists on that date."}Each rule carries the sentence a person reads. A database constraint that fires is mapped back to the author’s message, so a reader never sees the constraint name.
Handle empty values in a rule
Section titled “Handle empty values in a rule”A rule works like an SQL CHECK constraint. A check refuses the save only when its answer is
false. When a comparison reads an empty value, it has no answer, and the rule passes. In the
example above, an event without an end saves. An event whose end is before its start is refused.
The same applies to the parts of a rule:
notover a comparison with no answer also has no answer, so the rule still passes.check points >= 0 and points <= 15passes whenpointsis empty.require capacity when kind = limiteddoes not apply whilekindis empty.- A
boolcolumn on its own is never empty in a rule. An emptyboolcounts as false.
If a rule must refuse the empty case, say so in the rule. Use require when the value belongs
to a form field. The engine then puts the message on that field and marks the field as required
while the condition is true:
require reminder_minutes when reminders "Say how many minutes before an event you want to be reminded."check not reminders or reminder_minutes > 0 "Say how many minutes before an event you want to be reminded."Or write the test for emptiness into the check: check reminder_minutes is not empty and reminder_minutes > 0.
This applies only to check and require … when. In where:, access rules, deny when,
show_when: and computed values, a comparison with an empty value is false, and not over it
is true.
Refuse a booking that overlaps another
Section titled “Refuse a booking that overlaps another”Use a table check to refuse a row that conflicts with other rows. The rule holds for a form, a
transition and a sheet. Two examples follow. Two confirmed bookings of one room cannot overlap,
and a shift cannot confirm more volunteers than it needs.
Declare the relation
Section titled “Declare the relation”Declare a list column on the table that owns the key. It names the rows that point to this
row, and it stores nothing. This line goes in the rooms table. bookings.room is the
ref(rooms) column room of the bookings table.
bookings list(bookings.room) "Bookings"Write the rule
Section titled “Write the rule”Write the rule in the table that holds the rows. Write it on one line. A rule cannot continue on the next line.
check status != confirmed or not exists(room.bookings where status = confirmed and id != :this.id and starts_at < :this.ends_at and ends_at > :this.starts_at) at starts_at "This room is already booked in that time."The rule has these parts:
status != confirmed or …applies the rule to a confirmed booking only. Arequestedbooking can overlap another one until someone confirms it.room.bookingsgoes through theroomcolumn to thebookingscolumn of the room.id != :this.idleaves the booking out of its own comparison. Without it, an edit of a confirmed booking would collide with itself. For a new booking, the engine reads:this.idas0, so the comparison is true for every stored booking.starts_at < :this.ends_at and ends_at > :this.starts_atfinds an overlap. A bare name is a column of the other bookings.:this.…is a column of the booking that you save. The interval is half-open. A booking that starts when another one ends does not overlap it.at starts_atshows the message under the “Starts” field of the form. Withoutat, the message shows above the form.
Require the key and the times
Section titled “Require the key and the times”A booking with no room matches no room, so the rule above passes it. When a booking must have a room or a piece of equipment, add a rule for that:
check room is not empty or resource is not empty "A booking needs a room or equipment."Declare starts_at and ends_at as required. Inside the where, a comparison with an empty
value is false, so a booking with no end overlaps nothing.
Limit a count
Section titled “Limit a count”Use count(…) in the same way to limit the rows of a key. A shift can confirm as many
volunteers as it needs:
check status != confirmed or count(shift.roster where status = confirmed and id != :this.id) < shift.needed at status "This shift already has all the volunteers it needs."The shifts table declares roster list(assignments.shift) and an int column needed. The rule
counts the other confirmed assignments of the shift and compares the number with shift.needed.
Editing a confirmed assignment of a full shift passes. Confirming one more is refused.
count leaves out rows in a gone state. exists includes them. The compiler refuses a
where that names a gone state in a count.
What the person sees
Section titled “What the person sees”A person who saves a conflicting booking in a form sees the message under the field that the rule
names with at, with the values that they typed still in it. A rule without at shows its message
above the form. A transition runs the same rule on the row after the move. A transition has no
fields, so the message shows beside the transition buttons. In a sheet, the engine checks the rows
in the order of the save: the changed rows first, then the new rows. It checks each row against the
rows that it accepted before it. A row that fails is not written, and the check goes on to the next
row. The sheet shows the message of every row that fails, each on the cell of the field that the rule
names. A rule without at shows one sentence for each row, with the name of the row in it. If any row
fails, the save writes nothing, not even the rows that passed.
A row that fails does not count against the rows after it. If you free a seat in one row and take it in the next row, the save passes. If you take a full shift in one row, that row fails, and a later row that asks for a different seat is not blamed for it.
What the rule does not look at
Section titled “What the rule does not look at”The exists and the count in a rule ignore the person who saves. They apply no access rule,
no app scope and no :user. The compiler refuses a sensitive column in the where. Otherwise
the refusal would tell a writer who may not read the column whether another row has a given value.
The engine decides the rule inside the transaction of the write. Two people who save
conflicting bookings at the same moment cannot both pass. The read takes time in proportion to the
bookings of one room. On an Apple M3 Pro, the median was 0.05 ms at 100 bookings of one room and
1.1 to 3.0 ms at 10,000. For the full syntax and the compiler’s refusals, read
check.
Let the user create a missing option
Section titled “Let the user create a missing option”A ref field offers the rows that exist. Sooner or later the row somebody needs does not exist
yet, and they have a half-filled form on screen.
Name the page that creates one:
form of events { fields: title, category
field category { add_via: admin_category }}The engine draws a control beside the dropdown. The reader opens it, fills in the new category, and saves. The category becomes the selected option, and what they had typed is still there.
Three things this does not change, and they are the reason it is one line:
- The named page decides who may create a row. A reader that page does not admit sees no control. Do not write a second rule here.
- The named page decides what a save does — its fields, its checks, its refusals. The refusal appears in the dialog, in the words that page would have used.
- The save is validated the same way. The option the browser adds carries a row id, and the form checks every id it receives against the rows that reader may choose.
With JavaScript turned off the control is a link to the create page. The reader makes the row there, and the save returns to the form. The form comes back empty, because nothing keeps the half-filled one. Tell your users nothing about this: it is the walk they take today.
See add_via: for the
compile errors.
Hide a column from a role
Section titled “Hide a column from a role”Mark a column sensitive, then grant it in the rule of each role that can use it. A form
renders a control for a granted column only. See
Control who sees what.
What a form cannot write
Section titled “What a form cannot write”The engine owns some columns and a request can never supply one, even if it names it:
- the primary key.
- the audit columns
created_at,created_by,updated_at,updated_by. - a
slugand anunguessable. - the lifecycle state column.
- the metadata columns of an
img.
A column like this renders as a value and not as a control. A request that names one anyway is ignored, and the engine writes a log line naming the column and the reason.