How to make a custom Statamic form field
On this page
In February 2025 I needed to take a card payment inside an ordinary Statamic form submission, with no shop and no cart. Statamic has nothing like that built in, so I wrote a fieldtype for it. I covered the build setup and publishing of my addons in Building Statamic addons; this post is the part that post skipped, the actual shape of a fieldtype and what changes when it has to work inside a form.
The PHP class
A fieldtype is a class extending Statamic\Fields\Fieldtype. Trimmed, the Stripe one is this:
class StripePayment extends Fieldtype
{
protected $categories = ['special'];
protected $selectableInForms = true;
protected $keywords = ['stripe', 'payment', 'credit card', 'checkout'];
public function view()
{
return 'statamic-stripe-form-field::forms.fields.stripe_form_field';
}
public function extraRenderableFieldData(): array
{
return ['api_key' => config('statamic-stripe-form-field.key')];
}
protected function configFieldItems(): array
{
return [
'amount' => ['type' => 'text', 'validate' => ['numeric', 'min:0', 'required']],
'currency' => ['type' => 'text', 'default' => 'USD', 'validate' => ['required', 'size:3']],
'payment_description' => ['type' => 'text'],
'receipt_email_field_handle' => ['type' => 'text'],
];
}
}
Registration is one line in the addon service provider's bootAddon(): StripePayment::register(). The handle comes from the class name, so StripePayment is stripe_payment in blueprints. When I want a different handle I set protected static $handle = 'package_dimensions' on the class, and two of my older fieldtypes do it with a public static function handle() instead; both work.
configFieldItems() is the settings panel the editor sees in the blueprint builder. Everything the field needs to do its job at submit time (amount, currency, description, which other field holds the email for the receipt) lives there, not in code, so one addon serves a deposit form and a donation form with different settings.
Three things make it a form field rather than only a control panel field. $selectableInForms puts it in the list when someone builds a form blueprint; without it the field does not exist as far as forms are concerned. view() names the Antlers partial that {{ form:create }} renders when a template loops {{ fields }} and prints {{ field }}. And extraRenderableFieldData() adds variables to that view, in this case the publishable key, which the front end needs and the control panel does not.
The front-end view
The partial is where the payment actually happens. Stripe Elements mounts a card input, and when the card is complete it asks Stripe for a token and writes token.id into a hidden input named {{ handle }}. The form then submits like any other Statamic form; the server never sees a card number, only a token.
My first version put a <script> after the markup and waited for DOMContentLoaded. The second, the same evening, moved everything into an Alpine x-init on the wrapper, so the field initializes itself when it appears, wherever in the page it appears. The view is publishable with vendor:publish --tag=statamic-stripe-form-field-views so a site can restyle it.
The listener
The charge happens in a FormSubmitted listener:
public function handle(FormSubmitted $event)
{
$field = $event->submission->form()->fields->first(
fn ($field) => $field->type() === 'stripe_payment'
);
if (! $field) {
return;
}
$token = $event->submission->data()->get($field->handle());
if (! $token) {
return;
}
try {
$receiptUrl = $this->stripeService->handleFormPayment([...]);
$event->submission->data()->put('payment', $receiptUrl);
} catch (\Throwable $th) {
throw ValidationException::withMessages([$th->getMessage()]);
}
}
There is no $listen array anywhere. Statamic's AddonServiceProvider autoloads every class in the addon's Listeners folder and reads the type-hint on handle() to decide which event it wants. The listener finds the field by type, reads the token out of the submission under the field's handle, charges, and stores the receipt URL on the submission. If Stripe throws, it rethrows as a ValidationException, so the form shows the message and nothing is saved. That ordering, pay inside validation, is the whole reason to hook FormSubmitted rather than SubmissionCreated.
The first bug report was mine. The listener read $field->handle() before checking that $field existed, so every form on the site without a payment field blew up on submit. The guard went in the next morning.
The listener now takes StripeService through its constructor, so the Pest tests mock it and cover five cases: no field, a field without a token, a charge that stores a receipt URL, a charge with no receipt email configured, and a Stripe error wrapped in a ValidationException. The tests arrived in December, ten months after the field shipped, and the constructor injection that lets them mock Stripe came later still, with the Statamic 6 work. If your tests touch real forms, read Statamic feature tests write to your real content folder first.
The Vue side and augmentation
Every fieldtype also registers a control panel component, named after the handle:
Statamic.booting(() => {
Statamic.$components.register('stripe_payment-fieldtype', Fieldtype)
})
The component uses the Fieldtype mixin, which gives it value, config and meta as props and an update() method to push a new value back. The Stripe one is read-only, because a payment is not something an editor edits; it shows "No payment received" or a link to the receipt. My 3D model field is the other end of the scale: it copies this.value into local data(), renders a model-viewer with the options, and a deep watcher calls this.update(this.data) on every change. Anything the component needs that is not the value itself, like the asset container, comes from the PHP side's preload() and arrives as meta. The build configuration that makes any of this load inside the control panel is in the addons post.
On the PHP side, augment() decides what a template sees. The spectrum across my fieldtypes:
- The UPS package dimensions field returns the raw array. Four numbers and a boolean do not need help.
- The image hotspots field wraps each row in
Statamic\Fields\Valuesafter running the nested field configs through their own fieldtypes, so{{ hotspots }}in Antlers has augmented images and bard content, not strings. - The 3D model field leaves the value raw and ships a tag,
{{ 3d_model field="model" }}, that renders a Blade view with the script and themodel-viewerelement.
The rule I use: augment when the value is data a template will read, and write a tag when the output is markup with a script attached.
The short version
A custom form field is a fieldtype with $selectableInForms, a view() for the front end, and a listener on FormSubmitted that does the work and throws a ValidationException when it cannot. Keep the settings in configFieldItems(), keep the stored value small, and decide early whether the template needs augmented data or a tag.