Data and code
Forms
A form is a file in forms/ that returns a Form, like a Tempest config: an object with named arguments. From that one definition, Lapis renders the form, validates what visitors send, keeps spam out, stores the submissions and tells you about them by e-mail or webhook.
Defining a form
The fields come from Lapis\Forms\Fields, and the other arguments set the texts, the styling and what happens with a submission:
<?php // forms/contact.php use Lapis\Forms\{FieldGroup, Form}; use Lapis\Forms\Fields\{Checkbox, Email, Select, Tel, Text, Textarea}; return new Form( title: 'Contact', fields: [ 'name' => new Text('Your name', required: true, autocomplete: 'name'), 'email' => new Email('Your e-mail', required: true), 'phone' => new Tel('Phone'), 'topic' => new Select('Topic', ['appointment' => 'Appointment', 'question' => 'Question'], required: true), 'message' => new Textarea('Message', required: true, max: 2000), 'privacy' => new Checkbox('I accept the privacy policy', required: true), ], groups: [new FieldGroup(['name', 'email'], class: 'two')], // side by side, <div class="two"> submit: 'Send', submitClass: 'btn', successTitle: 'Thank you!', successText: 'We will get back to you soon.', successClass: 'done', successTitleClass: 'big', notifySetting: 'email', // e-mail to the address in a setting (and/or notify: ['…']) webhooks: ['https://hooks.example.com/…'], // JSON POST retentionDays: 365, // submissions are deleted after this rateLimit: 5, // per IP, per 10 minutes );
These field types are available: Text, Email, Tel, Url, Number, Date, Textarea, Select, Radio and Checkbox. Webhook URLs must use https://. Notifications go to notifySetting (the address in a global setting), to any addresses listed in notify, or both.
On a page
Put the form into a template with <x-form> and the name of its file:
<x-form name="contact" class="contact-form" />
Editors may change the labels, the help texts, the button and the success message on the page. The texts are the same on every page that shows the form.
The form works without JavaScript: it sends a POST, and the visitor comes back to the same page. The ?form= parameter of that answer keeps it out of the page cache. With htmx on the page, the form is swapped in place instead. Errors show next to their fields, and the typed data stays.
Spam protection
Every form is protected in several ways at once:
- a honeypot field, and a signed time trap for submissions sent within 3 seconds,
- a limit per IP address (
rateLimit), where the stored key is only a hash, - optionally a captcha.
Name one in lapis.config.php, and put its keys into .env. Without both keys it is off, so a local copy of the site works without them.
use Lapis\Forms\FriendlyCaptcha; use Lapis\Forms\HCaptcha; use Lapis\Forms\ReCaptcha; use Lapis\Forms\Turnstile; captcha: Turnstile::fromEnv(), // TURNSTILE_SITE_KEY, TURNSTILE_SECRET captcha: HCaptcha::fromEnv(), // HCAPTCHA_SITE_KEY, HCAPTCHA_SECRET captcha: ReCaptcha::fromEnv(), // v2, the box: RECAPTCHA_SITE_KEY, RECAPTCHA_SECRET captcha: ReCaptcha::fromEnv(version: 3), // v3, no box: a score of at least 0.5 (minScore:) captcha: FriendlyCaptcha::fromEnv(), // FRIENDLY_CAPTCHA_SITE_KEY, FRIENDLY_CAPTCHA_API_KEY (eu: true for EU)
| Captcha | What the visitor sees | Cookies |
|---|---|---|
| Cloudflare Turnstile | a small box, it mostly solves itself | none, as the service says |
| hCaptcha | a box, sometimes a puzzle | yes |
| reCAPTCHA v2 | the "I'm not a robot" box | yes (Google) |
| reCAPTCHA v3 | nothing, a score in the background | yes (Google) |
| Friendly Captcha | a small box that solves itself | none, as the service says |
The widget goes before the form's button, and the server asks the service before the submission is stored. A failed answer shows the form again with "The check failed. Please try again." When the service does not answer, the submission goes through to the other checks, so an outage loses no message. The reCAPTCHA v3 token is fetched when the visitor sends the form (with htmx too), and it counts when its score reaches minScore.
The keys stay in .env, not in the editor's settings: a secret there would be in the database and in every backup. A captcha that sets cookies is a service to list in your cookie notice. A captcha of your own implements Lapis\Forms\Captcha (enabled(), widget(), and verify(), which returns null when the service did not answer).
Bots get a success answer, so they do not learn anything, but nothing they send is stored or passed on.
Submissions
Submissions are stored in the database, without the visitor's IP address or user agent. They are deleted after retentionDays by ./lapis forms:prune, which runs daily.
When mail is configured, Lapis sends a notification with the visitor's address as Reply-To, and it posts the submission to the webhooks. Both happen after the visitor has the answer, so a slow mail server does not slow down the form. For your own handling, listen to the FormSubmitted event.
Editors read the submissions in the toolbar's Submissions panel. They can mark them read or unread, delete them, and download them as CSV (separated by semicolons, in UTF-8, and protected against spreadsheet formulas).
In tests
$this->formToken() returns the hidden fields that a valid test submission needs.