Forms
A form in Luxel is a group of Fields and an Action. The action sends the typed values to the server as an event. The server validates, decides and answers. The browser shows the answer in a Status zone and applies the local transitions you declared. There is no client-side validation logic and no onSubmit.
A complete form
‹Stack as="section" className="card"> ‹Row as="form" className="form"> ‹Field name="name" label="Name" maxLength={60} required autoComplete="off" /> ‹Field name="email" label="Email" maxLength={254} required autoComplete="off" /> ‹Action id="add" event="contact.add" status="result" busyLabel="Adding…" onSuccess={{ clear: ['name', 'email'], focus: 'name' }} className="button"> Add ‹/Action> ‹/Row> ‹Status id="result" errorClassName="notice notice--error" successClassName="notice notice--success" /> ‹/Stack>
// server/contract.ts (the contacts view) events: { 'contact.add': allow(['user']), }, messages: { added: 'Contact {0} added.', nameInvalid: 'Enter a name of 1 to 60 characters.', emailInvalid: 'Enter a valid email address.', },
// server/events.ts export const contactAdd = defineEvent(events.contactAdd, async ({ name, email }, ctx) => { const cleanName = name.trim(); const cleanEmail = email.trim().toLowerCase(); if (cleanName.length < 1 || cleanName.length > 60) return ctx.reject('VALIDATION', ctx.message('nameInvalid')); if (!EMAIL.test(cleanEmail)) return ctx.reject('VALIDATION', ctx.message('emailInvalid')); contactOps.add(ctx.session.userId as string, { name: cleanName, email: cleanEmail, addedAt: Date.now() }); // ...read the new page of contacts into `p` return ctx.set('contacts.rows', p.rows).success('added', cleanName).version(p.version); });
The handler's input type, { name: string; email: string }, is generated from the view: the fields of the form the action belongs to. A field the view does not send cannot be read.
Field
‹Field name="amount" label="Amount" type="decimal" maxLength={16} required placeholder="0.00" />
| Prop | Meaning |
|---|---|
| name | Zone id and input name, unique in the view, [a-z][a-z0-9]* |
| label | The visible label (a native ‹label>) |
| type | text (default), decimal, password, integer, date |
| min, max | For integer: bounds; a hint for the input, and re-checked on the server |
| maxLength | Default 512, the protocol limit |
| required | The field must not be empty |
| placeholder | Placeholder text |
| autoComplete | off, username, current-password |
| className, labelClassName | Classes of the input and of its label |
The types:
- text: any text.
- decimal: money or another decimal typed as text. It is never a float: the server parses it (see Money).
- password: a password input.
- integer: digits. min and max are re-checked on the server.
- date: the native date input. An ISO 8601 date (yyyy-mm-dd) travels.
type="email" or any other value is LUXEL-E011.
Values are always strings in the handler. An empty field arrives as ''.
Which fields an Action sends
By default an Action sends the Fields and Selects of the enclosing as="form" container (Stack or Row). To send other fields, name them:
‹Row className="search"> ‹Field name="q" label="Search" maxLength={40} autoComplete="off" /> ‹Action id="search" event="agencies.search" fields={['q']} status="result">Search‹/Action> ‹/Row>
An Action outside a form and without fields sends no input. fields naming something that is not a field is LUXEL-E013. One event sent with two different shapes (different fields, or with and without a parameter) is LUXEL-E021: use one event per shape.
Action
| Prop | Meaning |
|---|---|
| id | Zone id, unique in the view (required outside an item template, LUXEL-E009) |
| event | The server event; it must be declared in the contract (LUXEL-E007) |
| status | The id of the Status zone that shows this action's errors and confirmations |
| busyLabel | The label while the event is in flight, e.g. "Saving…" |
| busyClassName | The class of the in-flight indicator |
| onSuccess | Local transitions after a successful answer: clear (field names), focus (a field name or an action id) |
| fields | The fields to send (default: the enclosing form's) |
| param | In an item template only: the item key sent with the event (see Collections) |
| poll | This action is the view's refresh (see Polling) |
| className | Class of the button |
The children are the label. In an item template the label can be an item field.
onSuccess is presentation only. It never changes server state, and the server does not need to send it. It replaces the setState('') and ref.focus() you would write after a successful submit in React.
If you click before the runtime is ready, the click is queued. A second click while the event is in flight is dropped, like a double click.
Status
‹Status id="result" className="status" errorClassName="notice notice--error" successClassName="notice notice--success" />
A Status zone shows the answer of the actions that name it: the refusal message, or the success message of ctx.success(...). Each answer replaces what it showed before; an answer with no message clears it. Several actions may share one Status.
Validation is the server's
Validate in the handler. Refuse with ctx.reject(code, message):
if (!EMAIL.test(cleanEmail)) return ctx.reject('VALIDATION', ctx.message('emailInvalid'));
- code is one of VALIDATION, INSUFFICIENT_FUNDS, FORBIDDEN, NOT_FOUND. The other protocol codes belong to the framework.
- message is a message of the view (ctx.message(key, ...args)) or a plain string. Prefer messages: they are compiled templates, typed, and translatable (see Internationalization).
- For a write event, the refusal is stored with the event id, like any answer, so a retry of the same event gets the same refusal. Validate before you change anything.
Before your handler runs, the gateway and the framework re-check what the view declared: the input length (maxLength), required fields, integer bounds, valid dates, and the shape of select keys. A problem there gets one generic answer. Your handler checks the business rules.
required on a Field is therefore checked by the server too. The browser may also show the native hint, but nothing depends on it.
Write messages that say what to do, not which rule refused. For sign-in, answer the same message for an unknown user and a wrong password, as the template does.
Messages with placeholders
Messages live in the contract, per view. Placeholders are {0} to {9}:
messages: { added: 'Contact {0} added.', submitted: 'Guide {0} registered for {1}: {2}.', },
ctx.message('added', name) and ctx.success('added', name) take exactly one string argument per placeholder; a wrong key or count is a TypeScript error. The templates are public text (they are part of the compiled view); the keys stay on the server.