Contact form
A contact form is the smallest form worth shipping. This one validates each field with its own message, asks for a ticket number only when the subject is a support request, sends the values as JSON, and shows a server rejection on the field it names. The form is one config, so React and Radiant render the same data: only the renderer file differs.
Install
For React:
pnpm add @schema-engine/forms @schema-engine/forms-react @schema-engine/layout react react-dom zodThe React file registers the fields of the shadcn registry. Run pnpm dlx shadcn@latest add schema-engine/schema-engine/schema-form once: it copies the fields into your app and writes the registrations barrel that the file imports as @/components/schema-engine/forms/registrations (adjust the alias to your components.json). The page does not show those files, because they are yours once copied: Registry fields lists them and how to own them.
For Radiant:
pnpm add @schema-engine/forms @schema-engine/forms-radiant @schema-engine/layout @ecopages/radiant-ui @ecopages/radiant @ecopages/jsx @ecopages/signals zodRadiant also needs its JSX setup and stylesheets: Forms Radiant: installation and quick start. React needs "jsx": "react-jsx" in tsconfig.json. Copy the files below into one folder.
See it
Both forms render the config below. Submit the empty form to see each message, choose "Technical support" to show the ticket field, then submit a valid form: a notice says the message was sent.
The demos use this site's styled widgets, not the example's own files, and send to no real server: a valid submit shows the notice the config names.
The config
The config is data, so it could equally be a JSON file or a server's answer. rules validate a field and carry the message the visitor reads. The ticket field has a constraints entry that shows it only when subject equals support, and a field a constraint hides takes no part in validation or in the submitted values: a general question needs no ticket. A submit element is the button. The one show_notification entry in actions runs on form_submit_success, after the onSubmit callback resolved, so the visitor is told only once the message was accepted.
import type { FormConfig } from '@schema-engine/forms';
/**
* The contact form both renderers render.
*
* @remarks
* It is data, so it could equally be a JSON file or a server's answer. The ticket field shows only for a support
* request, and is validated only then: a field a constraint hides takes no part in validation or in the submitted
* values.
*/
export const contactForm: FormConfig = {
id: 'contact',
title: 'Contact us',
description: 'We answer within one working day.',
steps: [
{
id: 'details',
title: 'Contact us',
elements: [
{
$type: 'input',
id: 'name',
name: 'name',
label: 'Full name',
rules: [{ $type: 'required', message: 'Enter your name' }],
},
{
$type: 'input',
id: 'email',
name: 'email',
label: 'Email',
inputType: 'email',
rules: [
{ $type: 'required', message: 'Enter your email' },
{ $type: 'email', message: 'Enter a valid email address' },
],
},
{
$type: 'select',
id: 'subject',
name: 'subject',
label: 'Subject',
placeholder: 'Choose a subject',
options: [
{ value: 'general', label: 'General question' },
{ value: 'support', label: 'Technical support' },
],
rules: [{ $type: 'required', message: 'Choose a subject' }],
},
{
$type: 'input',
id: 'ticket',
name: 'ticket',
label: 'Support ticket',
placeholder: 'SUP-1024',
constraints: [{ field: 'subject', operator: 'equals', value: 'support', $type: 'show' }],
rules: [
{ $type: 'required', message: 'Enter your ticket number' },
{
$type: 'pattern',
value: '^[A-Z]{2,4}-[0-9]{3,6}$',
message: 'Use a ticket number such as SUP-1024',
},
],
},
{
$type: 'textarea',
id: 'message',
name: 'message',
label: 'Message',
rows: 5,
rules: [
{ $type: 'required', message: 'Write a message' },
{ $type: 'minLength', value: 10, message: 'Use at least 10 characters' },
],
},
{ $type: 'submit', id: 'send', name: 'send', label: 'Send message' },
],
},
],
actions: [
{
id: 'sent',
$type: 'show_notification',
message: 'Message sent. We answer within one working day.',
variant: 'success',
triggers: ['form_submit_success'],
},
],
};The submit handler
sendMessage is the same for both renderers. It receives the values, which passed validation, and the engine's fetch, so a CSRF policy applies without code here. A 422 answers with RFC 9457 Problem Details: fromProblemDetails turns it into the errors setServerErrors shows on the fields it names (Server errors). It returns whether the server accepted the message.
import type { FormSubmitHelpers, FormValues } from '@schema-engine/forms';
import { fromProblemDetails } from '@schema-engine/forms/server-errors';
/**
* Sends the message to the API and says whether it was accepted.
*
* @remarks
* The values are exactly those that passed validation, and `fetch` is the engine's, so a CSRF policy applies
* without code here. A 422 answers with Problem Details: it is shown on the fields it names, and the message
* counts as not sent. Any other failure throws, which the form reports as a failed submission.
*/
export async function sendMessage(values: FormValues, { fetch, setServerErrors }: FormSubmitHelpers): Promise<boolean> {
const response = await fetch('/api/contact', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(values),
});
if (response.status === 422) {
setServerErrors(fromProblemDetails(await response.json()));
return false;
}
if (!response.ok) throw new Error(`The server answered ${response.status}.`);
return true;
}The renderer
Each tab builds an engine, renders the config with FormRenderer, and calls onSent once the server accepted the message. The React engine registers the schemaFormRegistrations of the installed fields: an input, a textarea, a select and a submit button, each a registration with a $type the config names. Radiant ships these elements, so its engine registers the package's. Both engines register NotificationActionRegistration, the package's own, which only logs the message because the package cannot know how an app shows one; the demos on this page register one that shows a toast, and Submit, notify and reset registers one of your own.
import { createFormEngine, NotificationActionRegistration } from '@schema-engine/forms';
import { FormRenderer } from '@schema-engine/forms-react';
import { contactForm } from './config';
import { sendMessage } from './submit';
import { schemaFormRegistrations } from '@/components/schema-engine/forms/registrations';
export const engine = createFormEngine({
elements: schemaFormRegistrations,
actions: [NotificationActionRegistration],
});
/** Renders the form and calls `onSent` once the server has accepted the message. */
export function ContactForm({ onSent }: { onSent: () => void }) {
return (
<FormRenderer
engine={engine}
config={contactForm}
onSubmit={async (values, helpers) => {
if (await sendMessage(values, helpers)) onSent();
}}
/>
);
}import { NotificationActionRegistration } from '@schema-engine/forms';
import { createRadiantFormEngine, FormRenderer, schemaFormRegistrations } from '@schema-engine/forms-radiant';
import { contactForm } from './config';
import { sendMessage } from './submit';
export const engine = createRadiantFormEngine({
elements: schemaFormRegistrations,
actions: [NotificationActionRegistration],
});
/** Renders the form and calls `onSent` once the server has accepted the message. */
export function ContactForm({ onSent }: { onSent: () => void }) {
return (
<FormRenderer
engine={engine}
config={contactForm}
onSubmit={async (values, helpers) => {
if (await sendMessage(values, helpers)) onSent();
}}
/>
);
}Render it where the page needs it:
import { useState } from 'react';
import { ContactForm } from './contact-form.react';
export function ContactPage() {
const [sent, setSent] = useState(false);
return sent ? <p>Thanks, we will answer within one working day.</p> : <ContactForm onSent={() => setSent(true)} />;
}What the tests check
The example's tests render both files in a browser. An empty submit shows each message of the visible required fields and sends nothing. The ticket field appears for a support request and is required there. A valid submit posts the values as JSON and calls onSent. A 422 puts the server's message on its field and does not call onSent. The unit test checks the show_notification entry of the config in both engines, so a config that names an action an engine did not register fails.
Take it further
- Add a rule of your own: Custom and async rules.
- Show or hide more fields: Constraints.
- Wrap the form in a card or a wizard: Registration wizard and View recipes.
- Fetch the config from a server: A server-driven form.