A form is the one part of a site that isn’t built ahead of time. Each time a visitor sends a form, what they sent is a message. Each message is kept as an item of a collection, shown in the dashboard’s Inbox and emailed to the site’s owners. The example here is Slow Blink, a made-up cat cafe, with a form for asking to meet a cat.
The collection
Messages are kept as items of a collection, like any other items. A collection that a form fills must be private or
use publish. With private, no template can put what visitors send on the site. publish is for something like
reviews, which show on the site once an owner approves them. See
Showing what visitors send. collections/visits.yaml:
label: Visit requests
private: true
fields:
name: { type: text, required: true, max: 100 }
email: { type: email, required: true }
cat: { type: text, label: Which cat, max: 60 }
message: { type: text, multiline: true, required: true, max: 1000 }
status: { type: choice, options: [New, Replied, Adopted, Done], default: New }
order: [-created]
Fields that visitors don’t fill in, like status, are for the owner. Each of these fields starts at its default
value. Messages are kept for a year, unless the collection sets another length of time with keep.
A form can only fill simple fields: not a reference, image, richtext, list or object field. That’s why cat
is text here, not a reference to the cats collection.
The form
A file in forms/ says which collection the form fills, which fields visitors fill in, and what happens when a message
arrives. forms/visit.yaml:
label: Meet a cat
collection: visits
fields: [name, email, cat, message]
notify: [owner]
replyTo: email
success: Thanks! We'll write back within a day to find a time for you to visit.
notifyis who’s emailed about each message:owneris everyone who owns the site.replyTonames the field that holds the visitor’s email address. That address becomes the email’s Reply-To, so pressing reply writes to the visitor.successis the message the visitor sees once the form is sent, or{ redirect: /thanks }to send them to a page instead.forward: https://…also sends each message on as JSON, to a CRM or a spreadsheet’s webhook.
In the Inbox, under the form’s settings, the owner can change who’s emailed and what visitors see after sending.
On the page
The form block writes the <form> tag and everything a form needs besides its inputs: where the form is sent, the
Turnstile widget and other protections that stop spam, and a place for the message the visitor sees. You write the
inputs:
{{#form "visit" class="visit-form"}}
<label for="name">Your name</label>
<input id="name" name="name" autocomplete="name" />
<label for="email">Email</label>
<input id="email" name="email" type="email" autocomplete="email" />
<label for="cat">Which cat</label>
<input id="cat" name="cat" />
<label for="message">Tell us about your home</label>
<textarea id="message" name="message" rows="5"></textarea>
<button type="submit">Send</button>
{{/form}}
- Each input’s
nameis one of the form’s fields, and every required field needs an input. An input whosetypedoesn’t fit its field, or aselectwhose options aren’t the field’s choices, is an error. required,maxlength,typeand the like are added from the schema, so the browser checks the same things the server will.- The form is sent without leaving the page, which then shows the
successmessage. Without JavaScript, the page loads again with the message. - The element for the message has a
data-ht-form-statusattribute, which is set tosentorerror. Style the message with that attribute, and style inputs with a problem with[aria-invalid="true"].
Spam
Every form is protected in two steps, with nothing to set up.
First, bots are turned away before anything is kept. Every form has Cloudflare Turnstile, which almost nobody ever sees, and the form block adds other protections of its own.
Then an automated check reads what was written, and looks for the characteristics spam commonly has. A real message that happens to share one of them isn’t kept out on that alone.
A message the check flags is kept as spam, along with a short reason. It goes to Spam in the Inbox,
and it isn’t emailed to anyone or sent on to forward. Spam is kept for 30 days. Anyone in the site’s People can move a
real message back to the Inbox with Not spam.
The spam check is on for every form. spamCheck: false in the form’s file turns the spam check off. That suits a site
whose real visitors write what the check looks for, like a marketing agency’s site. An owner can turn the check off
too, with Check for likely spam in the form’s settings in the Inbox.
For more control, a form can have a filter: a server function with a form trigger, which
decides what happens to each message. The filter runs before the spam check. This filter keeps sales pitches out of
the owner’s email:
// functions/filter-visit.ts
export default defineFunction({
triggers: [{ form: "visit" }],
async run({ submission }) {
if (/seo services|backlinks|crypto/i.test(submission.message)) {
return { spam: true, reason: "Sales pitch" };
}
},
});
What a filter returns says what happens to the message:
| A filter returns | What happens |
|---|---|
| nothing | The message is kept and emailed as usual. |
{ spam: true, reason: "…" } |
It’s kept as spam, and nobody is emailed. |
{ spam: false } |
It isn’t spam, and the spam check doesn’t run. |
{ reject: "…" } |
It’s turned away, and the visitor is shown the message. |
{ notify: ["…"] } |
These addresses are emailed, in place of the form’s notify. |
{ set: { … } } |
The owner’s fields are filled in and checked against the schema. |
If the filter fails, the message is kept and emailed as usual, so no real message is lost. Each step a message goes through is in What happens when a form is sent.
Trying it
tekt startaccepts forms like the live site does. It prints the item it would keep and the email it would send, instead of keeping or sending them.- A preview treats every message as a test. The message is shown to whoever sent it, and isn’t kept or emailed.
- The live site is the only place a message reaches the Inbox and the owner’s email. Send a test message once the site is published.
Emails come from Hypertekt’s own address, with the site’s name on them. If the emails land in spam, the owner has a guide: When form emails land in spam, or don’t arrive.