Issue text

The engine never writes text for what a user enters or submits. Every issue of a field or a submission is data: what went wrong, where it came from, which rule and with which numbers. There is no default message. Turning an issue into words, in the user's language, is the application's job. (The messages of a form configuration error are for developers and are in English.)

The issue

import type { FormStateIssue, FormSubmissionIssue } from '@schema-engine/forms/state-container';

export const issues: Array<FormStateIssue | FormSubmissionIssue> = [
	{ type: 'minLength', source: 'validation', ruleId: 'password-min', params: { minLength: 8 } },
	{ type: 'typeMismatch', source: 'native', params: { inputType: 'email' }, message: 'Please include an "@"' },
	{ type: 'taken', source: 'server', message: 'This email is already registered' },
	{ type: 'actionFailed', source: 'submission', params: { actionId: 'save' } },
];
FieldHolds
typeThe problem, for a program: a rule type, a ValidityState flag of a native control, the server's code or the name of a submission failure.
sourceWho said so: validation (a rule), native (the browser), server (a response) or submission (the form as a whole, never held in the snapshot).
ruleIdThe id the author gave the rule, so two minLength rules on one form can read differently. Only on a rule's issue.
paramsThe numbers a sentence needs, under the names the platform uses: minLength, max, inputType. They are serializable.
messageText that someone wrote: the rule's author, the server or the browser. It is a fallback, never the first choice. The schema library's own text is never used.

A field's error is the issue to show plus every issue of that path in all. See Form state.

One formatter, one override

A formatter is a FormIssueFormatter: (issue, { path }) => string. There are two places to put one:

  1. On the engine, for every form: createFormEngine({ formatIssue }).
  2. On one rendered form, which replaces the engine's for that form only: the formatIssue prop of the React FormRenderer or FormProvider, or of the Radiant FormRenderer.

The text of an issue is the form's override, else the engine's formatter, else issue.message, else nothing. A formatter that returns an empty string falls back to message too. The rule type is never shown, so a field can be invalid with no text: a widget marks itself invalid from invalid, never from the presence of text.

In React, keep the formatter's identity stable: define it at module level or memoize it, because a new function on each render rebuilds the form state context.

A complete formatter

This one looks a translation up by the most specific key first (the rule's ruleId, then the problem's type), fills the numbers from params and falls back to the issue's own message. Text a server already wrote for people is shown as it is. t looks the first of its keys up in a catalogue and fills its {name} placeholders; it lives in a helper that this page does not show, so swap it for your i18n library. The keys are form.rules.<ruleId>, form.<source>.<type> and form.fields.<path>, and the texts below come from the catalogue.

import type { FormIssueFormatter } from '@schema-engine/forms/state-container';
import { createFormEngine } from '@schema-engine/forms';
import { t } from './i18n.helper';

export const formatIssue: FormIssueFormatter = (issue, { path }) => {
	if (issue.source === 'server' && issue.message) return issue.message;

	const keys = [
		'ruleId' in issue && issue.ruleId ? `form.rules.${issue.ruleId}` : undefined,
		`form.${issue.source}.${issue.type}`,
	].filter((key): key is string => key !== undefined);

	return t(keys, { ...issue.params, field: t([`form.fields.${path}`]) ?? path }) ?? issue.message ?? '';
};

/** Your engine: add the elements you register beside `formatIssue`. */
export const engine = createFormEngine({ formatIssue });

The catalogue and the engine's formatter give these texts:

IssueText shown
minLength, rule password-min, minLength: 8Use at least 8 characters for your password
minLength, rule bio-min, minLength: 20Use at least 20 characters (falls back to the type key)
required, rule email-required, at emailEmail is required
typeMismatch, native, inputType: 'email'Enter a valid email
taken, server, message: 'This email is already registered'This email is already registered
actionFailed, submission, at FORM_ROOTSomething went wrong. Please try again.

What gets formatted

  • The shown error of every field, and each entry of its all list.
  • Warnings, with the same function. A warning never blocks.
  • Submission failures, as issues of the form itself: the path is FORM_ROOT and the source is submission.

Where to read the text

BindingThe textThe raw issue, to word it yourself
React fielduseFormField(config).error and .warninguseFormField(config).issue
React, anywhereuseFormContext().formatIssue(issue, { path })useFormField(path).issue, or the container snapshot
Radiant fieldRendered by rui-field through rui-form's formatErrorThe container snapshot
Radiant custom elementnew FormController(this).formatIssue(issue, { path })controller.select((snapshot) => snapshot.errors[path])

useFormContext().formState.errors holds the texts, flat by path. In an action, context.errors holds the same issues by path, each with its all list.