Skip to content

Getting started ​

This page takes you from nothing to a form on a page of your own, then to a form you designed yourself, then to its answers in your hands. Follow it from top to bottom. No server of your own is needed until the last step.

You need Node 22 or newer, npm, git and Python 3 (only to serve a folder; any static file server does the same).

Get the files ​

Datum is a folder of files you host. For now you build it from the repository:

sh
git clone https://github.com/argusvane/datum.git
cd datum

npm run setup -- engine packages/quickjs-wasm-host packages/pdf-kernel packages/datum
npm run datum:build

The installs and the build take a few minutes. When the last command ends, the folder packages/datum/dist/ holds everything the form and the builder load. Files you host says what each file is.

Make a site folder ​

Make a folder for your site beside the clone, with the built folder in it as datum/, and a place for forms:

sh
mkdir -p ../my-site/forms
cp -R packages/datum/dist ../my-site/datum
cd ../my-site

Save the sample form as forms/contact.json: contact.json. A form is one JSON file, the definition. This one has four fields and a Send button.

The sample form
json
{
  "id": "contact",
  "version": "1.0.0",
  "schemaVersion": "1",
  "localization": {
    "defaultLocale": "en",
    "supportedLocales": [{ "code": "en", "label": "English" }]
  },
  "root": {
    "id": "root",
    "type": "form",
    "configuration": { "title": "Contact us" },
    "children": {
      "content": [
        { "id": "fullName", "type": "text", "configuration": { "label": "Full name", "required": true } },
        {
          "id": "email",
          "type": "text",
          "configuration": {
            "label": "Email",
            "required": true,
            "pattern": "[^@\\s]+@[^@\\s]+\\.[^@\\s]+",
            "patternMessage": "Enter an email address."
          }
        },
        {
          "id": "topic",
          "type": "radio",
          "configuration": {
            "label": "What is this about?",
            "options": [
              { "value": "question", "label": "A question" },
              { "value": "problem", "label": "A problem" },
              { "value": "other", "label": "Something else" }
            ]
          }
        },
        { "id": "message", "type": "textarea", "configuration": { "label": "Message", "maxLength": 1000 } },
        { "id": "submit", "type": "button", "configuration": { "label": "Send", "action": "submit" } }
      ]
    }
  }
}

Put the form on a page ​

Save this as index.html in the site folder:

html
<!doctype html>
<meta charset="utf-8">
<title>Contact</title>
<link rel="stylesheet" href="/datum/datum-form.css">
<script type="module" src="/datum/datum-form.js"></script>

<datum-form src="/forms/contact.json"></datum-form>

Serve the folder and open localhost:8080 in a browser:

sh
python3 -m http.server 8080

You should see a form titled "Contact us" with a name, an email, three choices, a message box and a Send button. Press Send with nothing filled in: the form lists two errors and marks the two required fields.

If it does not work:

  • A blank page. The page was opened from disk. A browser loads a module only over HTTP, so open the address above, not the file.
  • "This form could not be loaded." The element could not fetch the definition. Check that forms/contact.json is in the site folder. The browser's console names the address and the reason.
  • Nothing where the form should be. datum/datum-form.js was not found. Check that the built folder was copied in as datum/.

Take the submission ​

When the person presses Send and the answers pass, the form raises datum-submit. Add this under the form, and reload:

html
<pre id="answers"></pre>

<script type="module">
  const form = document.querySelector("datum-form");
  form.addEventListener("datum-submit", ({ detail }) => {
    if (!detail.accepted) return;        // the form shows its own errors
    document.getElementById("answers").textContent =
      JSON.stringify(detail.submission, null, 2);
  });
</script>

Fill in a name and an email and press Send. The page prints what the form hands over:

json
{
  "submission": {
    "locale": "en",
    "values": { "email": "ada@example.org", "fullName": "Ada Lovelace" }
  },
  "versionContext": {
    "client": { "…": "what the browser ran" },
    "form": { "id": "contact", "version": "1.0.0" }
  }
}

That object is the envelope: the answers under submission.values, and the form's id and version, which say which definition they were given against. Its keys are always in the same order, so the same answers always make the same text. Submissions and replies describes all of it.

Design a form of your own ​

The builder is one more element from the same folder. Save this as builder.html:

html
<!doctype html>
<meta charset="utf-8">
<title>Builder</title>
<link rel="stylesheet" href="/datum/datum-builder.css">
<script type="module" src="/datum/datum-builder.js"></script>

<datum-form-builder></datum-form-builder>

Open localhost:8080/builder.html. It opens on a new form that holds a Submit button and nothing else.

  1. Click a component in the panel on the left, or drag it onto the form. Click it on the form to edit its label and its settings on the right.
  2. Press Try it to fill the form in as a person would.
  3. Open the menu (⋮) at the right end of the bar and choose Download form. The browser saves the definition as a JSON file.
  4. Move that file into forms/, point src in index.html at it, and reload the page.

Open form file… in the same menu loads a definition back in, so forms/contact.json can be the starting point instead. Building a form walks the whole builder.

Send it to a server ​

Until now nothing has left the browser. To keep the answers, the page delivers the envelope to an endpoint of yours. Replace the script from Take the submission with this one, and add the confirmation attribute to the form:

html
<datum-form src="/forms/contact.json" confirmation></datum-form>

<script type="module">
  const form = document.querySelector("datum-form");
  form.addEventListener("datum-submit", ({ detail }) => {
    if (!detail.accepted) return;
    detail.waitUntil(deliver(detail));   // the form stays locked until this settles
  });

  async function deliver(detail) {
    try {
      const response = await fetch("/api/submissions", {
        method: "POST",
        headers: { "content-type": "application/json" },
        body: JSON.stringify({ document: detail.signed ?? detail.submission,
                               submissionKey: detail.submissionKey })
      });
      const reply = await response.json();
      if (response.status === 422) {     // { errors: [{ field, code, message }] }
        form.setErrors(reply.errors.map((error) => ({
          componentId: error.field, code: error.code, message: error.message })));
      } else if (!response.ok) {
        throw new Error("The server answered " + response.status);
      }
      return reply;                      // `confirmation` reads its reference
    } catch {
      form.setErrors([{ code: "delivery", message: "Your answers could not be sent. Try again." }]);
    }
  }
</script>

/api/submissions is not part of the folder: it is the endpoint your server provides. With the static server from this page there is none, so pressing Send now shows "Your answers could not be sent." in the form. That is the failure path working.

What the script does:

  • waitUntil keeps the form locked, with a "Submitting…" line, until the delivery settles.
  • The body is the envelope as document and submissionKey, a key that stays the same for every retry of one attempt, so the server can keep one record for it.
  • A 422 carries the server's own errors, which setErrors puts on the fields.
  • An accepted reply with a reference is shown by the form's own confirmation, because of the confirmation attribute.

The route, the body and both replies are the defaults of Datum's server hosts, so this page works against one unchanged. A server host finds the same definition by its id and version and checks the answers again in the sandbox: it never takes the browser's word.

Where next ​

MIT licence. Your forms, your data, your keys.