Skip to content

FormRegistry

The FormRegistry manages all form controllers. Access via the exported formRegistry singleton.

import { formRegistry } from 'formlayer';

For one form, skip the registry:

import { createFormController, registerDefaultValidators } from 'formlayer';
registerDefaultValidators();
const form = createFormController(document.getElementById('contact')!, submitFn);

Register globally via module-level APIs (not on the registry):

import { registerValidator, registerFieldType } from 'formlayer';
registerValidator({ type: 'MyValidator', validate(value, options) { ... } });
registerFieldType('slider', () => import('./fields/slider'));

Discovers and registers all matching forms.

formRegistry.init({
submitFn: FormSubmitFunction,
root?: ParentNode, // default: document
formSelector?: string, // default: 'form[id]'
controllerOptions?: FormControllerOptions,
}): void

register(formEl, submitFn, controllerOptions?)

Section titled “register(formEl, submitFn, controllerOptions?)”

Registers a single form element. Returns the form controller API. No-ops if already registered.

const api: FormControllerApi = formRegistry.register(formEl, submitFn);

Destroys the controller and removes the form from the registry.

formRegistry.unregister('my-form');

Returns the FormControllerApi for a registered form, or undefined.

const form = formRegistry.get('contact');

Returns a Map<string, FormControllerApi> of all registered forms.

Subscribe to registry-level events.

formRegistry.on('form:registered', ({ formId }) => { ... });
formRegistry.on('form:unregistered', ({ formId }) => { ... });

Registers a form-level plugin factory. Applied to all forms registered after this call.

formRegistry.registerFormPlugin(() => import('./plugins/analytics'));

The registry is also available globally as window.__FormsModule (browser environments only).