formController tools
The formController exposes various methods for managing forms. Each method has a specific role — together they manage the form's lifecycle. register registers inputs and wires event handlers; getValues and getErrors read current form state and errors. Use setValues and setErrors to inject external data or manipulate state, and handleSubmit to validate and run callbacks on successful submission. These tools enable declarative, maintainable form logic and improve developer productivity and code quality.
register#
register registers an input with the Sicilian controller and auto-connects event handlers. As the type definitions below show, it supports many input types and provides type safety in TypeScript. The ExtractKeys<T> variant restricts allowed field names to those defined in initValue or validator, preventing typos. The string variant allows dynamic field addition. For radio, value is required to distinguish radio buttons sharing the same name. You may also pass a validate object per-field for custom rules.
The object returned by register (the IRegister interface) provides props and handlers to integrate with React inputs. onChange, onBlur, and onFocus update form state and optionally trigger validation. SicilianEvent handles DOM and custom events. name and id identify fields; type, checked, and value reflect native input props. Apply them to JSX with the spread syntax {...register({ name: "email" })} to reduce boilerplate.
Values and Errors#
The formController provides access to current values and errors. getValues returns all values or a specific field; getErrors returns field error messages. These functions can be subscribed to so components re-render when observed values change. setStore and getStore expose low-level store access for advanced scenarios. Note:
The objects returned by getValues and getErrors reflect global state, which can cause re-render cascades similar to Context API patterns. To avoid broad re-renders, getValues and getErrors accept optional field names to subscribe to specific fields; without args they return complete objects.
setValues and setErrors update parts of the form state or error state, enabling fine-grained updates useful for performance optimization.
handleSubmit#
handleSubmit accepts a callback invoked with the current formState and the event object. It internally calls e.preventDefault() to prevent navigation on form submit.
You can implement submit logic by manually reading formState from getValues, but handleSubmit offers additional safety and conveniences.
- If any unresolved error message remains,
handleSubmitcancels submission to prevent unwanted data from going to the backend. - Similarly, if all inputs managed by the
formControllerare empty, submission is canceled to avoid accidental HTTP requests.
Even if clearFormOn includes ["submit"], the form will not clear if submission fails.
handleServerAction#
handleServerAction handles server actions or server functions to submit form data and process responses. It integrates with server-side async functions and lets you provide a server action function that receives form data and an event. Validation runs before the server action is invoked; if validation fails, the server action won't be called. Optionally provide a callback to handle success or error responses from the server.
Typical server action functions accept a FormData argument, but handleServerAction converts form data to an object before calling the server action, making server-side handling easier and avoiding direct FormData manipulation. The event object is also passed for additional context.
This design simplifies form submission and server communication using handleServerAction.