Conditional fields
A custom field can depend on other answers: on its Conditions tab an editor adds
rules such as "Onderwerp is equal to anders" and chooses whether all of them or
any of them must hold. The field renders the rules as JSON in a data-conditions
attribute, which resources/js/form-builder.js evaluates to show or hide it; the
form loads that script itself. A hidden field is disabled, so it is not posted
and what the visitor typed is back when it shows again. On the server, a
required conditional field is only required while its conditions show it.
A title or text block can have conditions too, on its wrapper. It has no answer,
so no rule looks at it; it shows while its rules hold for the fields that show.
A field type of your own without an answer opts in with canHaveConditions().
field_conditions set to false in the config leaves the Conditions tab out
of the builder; conditions a field already has, or gets in code, still apply.
What a rule can test depends on the field it looks at, through the field type's
getConditionOperators(): any answer can equal a value or be empty, a number
can also be greater than, at least, less than or at most a value, a date can
be before, on or before, after or on or after a date, and a checkbox is ticked
or not. getConditionPhrase() gives the words a rule reads as, and
getConditionValueComponent() the input its value is filled in with: the
field's own choices, a date picker for a date, a number field for a number and
text for the rest. The same rules
decide when an email notification goes out and which outcome follows a
submission.
In a front end of your own
A front end in React, Vue or anything else, such as one on Inertia, uses the
same rules through resources/js/conditions.js. It has no dependencies, and
resources/js/conditions.d.ts types it. Point an alias at the package, the
way Ziggy is often imported from vendor:
// vite.config.js
resolve: {
alias: {
'@form-builder': path.resolve(__dirname, 'vendor/van-ons/filament-form-builder/resources/js'),
},
},// tsconfig.json, compilerOptions
"paths": {
"@form-builder/*": ["./vendor/van-ons/filament-form-builder/resources/js/*"]
}Hand the page the conditions with the fields. $form->getFieldConditions()
gives the fields that have any, by key. A title or text block has no key: hand
its own $field->getConditions()->toArray() along with it and check it with
isShown().
return Inertia::render('Contact', [
'form' => [
'id' => $form->id,
'fields' => $fields, // however your front end reads them
'conditions' => $form->getFieldConditions(),
'honeypot' => $form->getHoneypot(),
],
]);| Function | Gives |
|---|---|
hiddenKeys(conditions, values) | The keys of the fields to hide, as a Set. A hidden field counts as empty for the others, so a field that depends on a hidden one hides too |
readValues(form) | The answers in a form element by key, the way it would post them |
passes(conditions, values) | Whether one field's conditions hold |
isShown(conditions, values, hidden) | Whether a title or text block shows: its conditions hold for the answers of the fields that show |
matches(value, operator, expected) | Whether an answer meets one rule |
They read the rules exactly as the server does, so a field the page hides is
one the server does not require. With Inertia's <Form>, which posts what is
in the form element, reading the answers on every change is enough:
import { Form } from '@inertiajs/react'
import { useState } from 'react'
import { hiddenKeys, readValues } from '@form-builder/conditions'
export default function ContactForm({ form }) {
const defaults = Object.fromEntries(form.fields.map((field) => [field.key, field.defaultValue]))
const [hidden, setHidden] = useState(() => hiddenKeys(form.conditions, defaults))
return (
<Form
action={`/filament-form-builder/${form.id}/submit`}
method="post"
onChange={(event) => setHidden(hiddenKeys(form.conditions, readValues(event.currentTarget)))}
>
{form.fields
.filter((field) => !hidden.has(field.key))
.map((field) => <Field key={field.key} field={field} />)}
</Form>
)
}A field left out of the form is not posted. With useForm() instead, pass its
data as the values and leave the hidden keys out with transform().
Inertia follows the 303 the route answers with, back to the page with the
thank-you message in the session as submit_notification_content, so share
that with the page; or on to the form's redirect. A redirect to another site
comes as a 409 with X-Inertia-Location, which has Inertia send the browser
there itself. Every form type works this way, so a front end of your own needs
no type of its own.
A session that expired is answered the same way, not with a 419: back to the
page, with the form's id in the session as filament_form_builder_expired.
Share that with the page too and show
__('filament-form-builder::general.session_expired') at that form, or the
visitor sees the form again without knowing it was not sent.
The server turns a form away without the honeypot unless it is off.
$form->getHoneypot(), above, is null when the form has none; set its two
fields with resources/js/honeypot.js:
import { honeypotInput } from '@form-builder/honeypot'
{form.honeypot && (
<>
<input {...honeypotInput(form.honeypot)} />
<input type="hidden" name={form.honeypot.tokenField} value={form.honeypot.token} />
</>
)}With useForm(), start its data with honeypotFields(form.honeypot) and bind
the hidden field's value to it, so a bot that fills it in posts what it typed.
The token holds the moment the page was made, so hand a fresh one with every
page: a form sent within honeypot.min_seconds of it counts as a bot. Turn the
button off while Inertia's processing is true; the server stores a double
click once either way.
