Forms (2)
Select
A Select shows options the server sends. It names an options() path of the contract:
‹Select name="category" label="Category" options="transactions.categories" />
// contract state: { transactions: { categories: options({ max: 9 }), // ... }, }, events: { 'transactions.apply': allow(['customer'], { effect: 'read', refs: { category: LOCAL } }), },
// load return ctx.snapshot(version, { 'transactions.categories': [ { key: 'all', label: 'All categories' }, { key: 'c1', label: 'Groceries' }, ], // ... });
- The value that travels is the option's key, never its label.
- A select's value is an id, so the event needs a refs entry for it (LUXEL-E045): LOCAL when the key names no resource (a sort order, a category of a closed list), or a resource kind whose scope function re-checks the key against the session (see Server).
- Treat the key as untrusted: map it back to your closed list on the server, and fall back to a default for anything else.
- Outside a collection, the options can change with any answer: ctx.set('guide.agencies', found) after a search the server ran. In a collection row, the options are fixed for the page (see Collections).
- options({ max }) above the maxOptions policy is LUXEL-E047: search on the server instead.
luxel check counts a Select as a Field for policies.allowedComponents (both are inputs); the template's list already allows both.
Money: integer cents, parsed on the server
Money is always an integer number of cents. Never a float, anywhere.
- Typing an amount: use ‹Field type="decimal">. The browser sends the text as typed.
- Parsing: the handler turns it into cents, or refuses it.
- Showing an amount: a money() path takes integer cents; the host formats it (see Internationalization).
/** "1234.5" or "1234,50" → 123450 cents; anything else → null. No float arithmetic. */ function parseCents(text: string): number | null { const match = /^(\d{1,9})(?:[.,](\d{1,2}))?$/.exec(text.trim()); if (!match) return null; return Number(match[1]) * 100 + Number((match[2] ?? '').padEnd(2, '0')); } export const transferSubmit = defineEvent(events.transferSubmit, async ({ recipient, amount }, ctx) => { const amountCents = parseCents(amount); if (amountCents === null || amountCents === 0) return ctx.reject('VALIDATION', ctx.message('amountInvalid')); // ...debit amountCents in the database, in this event's transaction return ctx.set('account.balance', newBalanceCents).success('paymentAuthorized', recipient); });
‹Text bind="account.balance" label="Available balance" className="balance__value" />
ctx.set on a money() path refuses anything that is not a safe integer.
Forms that span several rows
A form with lines (an order, a delivery guide) is a collection of rows with inputs. Each line is recalculated by the server when a field is left. See Collections.