formController tools
The formController exposes various methods for managing forms. Each method has a specific role — together they manage the form's lifecycle. useRegister registers inputs and wires event handlers; useFields reads field values, errors, and meta. 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.
useRegister#
useRegister 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 in the options.
The object returned by useRegister (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 {...useRegister('email')} to reduce boilerplate.
Values and Errors#
useFields provides access to current values and errors along with field meta. It can subscribe to the entire form or a specific field. setStore and getStore expose low-level store access for advanced scenarios. Note:
Calling useFields() without arguments subscribes to all fields, which can trigger broader re-renders. Prefer useFields(name) for fine-grained subscriptions.
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 via useFields(), 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.
useFields#
The useFields hook allows selective subscription to individual field states. Using this hook, you can subscribe to specific field values or errors instead of the entire form state, preventing unnecessary re-renders and optimizing performance.
The useFields hook takes a field name as an argument and returns a state object for that field. The returned object includes value, error, touched, dirty, and isValid. This allows components to use only the necessary field information without directly accessing the global form state.
- Performance optimization:
useFields(name)subscribes only to a specific field, so components re-render only when that field changes. This effectively prevents unnecessary re-renders caused by changes in overall form state. - Type safety: In TypeScript environments,
useFieldsprovides accurate type inference based on field names. Using incorrect field names causes compile-time errors, preventing runtime errors in advance. - Convenient usage: The state object returned by
useFieldscontains value, error, and meta for the field, allowing you to display status without separate state management logic.