Forms and submissions

A working form is three rows and one endpoint: a template that declares what the form asks, a form that gives those fields a path to be posted to, and a submission the platform writes for every valid answer. The forms API is the endpoint; the submissions come back through the admin API.

The three pieces

Piece Resource What it decides
Template form-templates The fields, their validation, and which two emails a submission sends
Form forms The path that receives the post, the template it asks for, and the message a visitor sees
Submission form-submissions The answers, as they arrived — read and deleted, never edited

A template first, because a form names one:

curl -X POST "$ADMIN_API/api/form-templates/save" \
  -H 'content-type: application/json' \
  -b cookies.txt \
  -d '{
    "templateId": "<uuid>",
    "siteId": "<site-id>",
    "name": "Contact",
    "alias": "contact",
    "content": "[{\"name\":\"email\",\"fieldTemplate\":\"emailEditorTemplate\",\"prompt\":\"Email\",\"required\":\"true\",\"requiredMessage\":\"We need an address to reply to\"},{\"name\":\"message\",\"fieldTemplate\":\"textAreaEditorTemplate\",\"prompt\":\"Message\",\"required\":\"true\",\"maxLength\":2000}]",
    "confirmationEmailTemplate": "contact-confirmation",
    "submissionEmailTemplate": "contact-notification"
  }'

Two keys there are about mail, not about the fields: confirmationEmailTemplate goes to the person who submitted, submissionEmailTemplate goes to the form's own recipients. Both name an email template (Admin API reference — email-templates).

Then the form, which points at the template and claims a path:

curl -X POST "$ADMIN_API/api/forms/save" \
  -H 'content-type: application/json' \
  -b cookies.txt \
  -d '{
    "formId": "<uuid>",
    "siteId": "<site-id>",
    "name": "Contact us",
    "path": "/contacts",
    "templateId": "<form-template-id>",
    "submissionAction": 0,
    "successMessage": "Thank you — we answer within one working day."
  }'

path is the address a post is sent to, and successMessage is what a visitor is shown when the answer is valid. submissionAction is required by the save and redirectUrl is accepted, but neither changes what the endpoint answers today: a valid submission answers its success message, not a redirect. A form row may also carry flat content values of its own; they are read back as fields and are there for the theme to render, not for submissions.

What a field means to a form

A form's field declarations are the same fieldTemplate vocabulary as any template (Templates and fields), with four keys that matter at submission time:

Key Effect
required Any truthy value makes the field mandatory (true, "true", "checked"). Missing means undefined, null, an empty string, false, 0 or NaN
requiredMessage The text reported for a missing value
minLength / maxLength Length bounds, checked as one rule
fieldTemplate emailEditorTemplate is checked as an email address; recaptcha2Template is checked against the captcha service

Validation runs per field, in one order — required, length, email, captcha — and every error is returned, not just the first:

Kind Where the message comes from
Missing required value requiredMessage, else Field '<name>' not found
Length Built from the bounds
Not an email Built from the field name
No captcha response requiredMessage — check the template sets one
Captcha verification failed verificationMessage

The captcha checks the g-recaptcha-response value against the site's private key, which lives in the site's settings as reCAPTCHA2.PrivateKey. A template that asks for a captcha and a site that has no key configured refuse every submission: the check cannot succeed, so the field reports its failure.

Only the fields the template declares are stored. An input a visitor sends that the template does not name is not saved, not validated, and not visible anywhere afterwards — which is what keeps a submission a fixed shape.

Submitting

A site that serves its own pages receives the submission at the form's own path — POST /contacts for the form above — and a theme can render the answer through a layout named form_ajax_response.

Headless, the same form is reached through the forms API, which takes the form's path as a query parameter. $FORMS_API below is that API's base URL:

curl -X POST "$FORMS_API/forms?path=/contacts" \
  -H 'content-type: application/json' \
  -H 'accept: application/json' \
  -d '{ "email": "reader@example.com", "message": "Do you ship to Almaty?" }'

The path is matched tolerantly: with or without a trailing slash both reach the same form. A post to a path no form owns answers 404. On a site that serves its own pages the body is not read at all before the lookup, so such a post stays available to whatever else serves that path.

Two things decide the shape of the answer, and a JSON client should ask for one explicitly:

Client asked for Valid Invalid
JSON (?format=json, or accept: application/json without text/html) 200 with the success message and the submission number 422 with the field errors
HTML (a browser submit) 200, or the theme's AJAX layout when the site has one 422, or the same layout

A valid JSON answer:

{ "is_valid": true, "message": "Thank you — we answer within one working day.", "number": 1042 }

An invalid one:

{
  "is_valid": false,
  "message": "We need an address to reply to",
  "errors": [
    { "field": "email", "msg": "We need an address to reply to" }
  ]
}

message repeats the first error, so a client that only wants one line can print it. An invalid submission writes nothing and sends nothing.

What a submission holds

A valid submission becomes one row: the form, the visitor's address and user agent, a number, the date in both the site's clock and UTC, an isSpam flag, and the field values keyed by the names the template declared.

The number comes from the platform's numbering counters: one series per site for the form numbering task, so two forms on one site draw from the same series. The field values are resolved before they are stored: a literal field takes the template's value, a markdown or textile field is rendered, a numeric or date field is converted according to its valueType, a selection or a text field keeps what the visitor sent, a checkbox keeps the submitted value as it arrived, and a multiple selection is a list of the chosen values — one value if one box was ticked, and nothing if none was. For the record, isSpam is written as false for every submission — the captcha is the only gate, and marking submissions as spam is not part of this surface.

Reading them back:

Call Answers
GET /api/form-submissions/?formId=<form-id>&skip=&take= The submissions of one form, in a plain window — this surface has no filter language
GET /api/form-submissions/{id} One submission, with its values as fields
POST /api/form-submissions/info with { "ids": [...] } Up to 100 submissions by identifier
DELETE /api/form-submissions/{id} Removes it. A submission cannot be edited: it says what was sent

Mail

Both letters are composed after the row is written, so a broken email template never costs you the submission. That also means the client is not told the outcome of the mail: a valid answer says the submission was accepted, and a letter that failed to compose or to deliver is visible in the server log, with the template name and the site.

Attribution

@snolla/utm-forms is a browser helper for marketing attribution: it reads UTM parameters, referrers and the landing page, keeps them in cookies, counts visits, and hands them to your submit as utm_source, utm_medium, utm_campaign, utm_term, utm_content, initial_referrer, last_referrer, initial_landing_page and visits_count. It works with three or four lines of plain JavaScript, and the same values can be written into hidden inputs.

Those values are stored like any other field, which means the form template has to declare them — a hidden field is the natural kind. An attribution value whose field the template does not declare is dropped like any undeclared input.

Related