Overview
useNSForm (or useForm) is a comprehensive form management hook that handles form state, validation using Yup schemas, automatic rendering, and submission. It supports all Grauity form components and provides a declarative API for building complex forms.Basic Usage
Form Field Types
The hook supports all Grauity form components through theFormFieldType enum:
Complete Example
Validation Modes
Custom Submit Button
Custom Form Fields
Conditional Props
Hook API
Parameters
The form configuration object containing field definitions, initial state, and validation schema.Properties:
fieldNames: Array of field name stringsinitialState: Object with field names as keys and initial valuesrows: Array of form row configurationsschema: Yup validation schema (optional)
CSS styles applied to each form row.
When to validate form fields. Options:
FormValidationType.ON_BLUR: Validate on blurFormValidationType.ON_CHANGE: Validate on changeundefined: Validate on submit only (default)
Whether the form is in mobile view. Affects layout and column behavior.
Whether to focus the first field with an error after validation.
Whether pressing Enter should submit the form.
Whether to show the default submit button.
Props passed to the default submit button.
Callback fired when the form is submitted with valid data.
Return Values
The current state of all form fields.
The rendered form component. Place this in your JSX to render the form.
Current validation errors for each field.
Function to manually validate specific fields.
Function to manually submit the form. Validates before calling
onSubmit.Function to update form data programmatically.
Form Configuration Types
FormRow
FormField
Yup Validation
The hook integrates seamlessly with Yup for schema validation:For dropdown and combobox fields in single-select mode, use
object().nullable() in your Yup schema since they return objects.Best Practices
- Organize fields logically: Group related fields in rows
- Use appropriate field types: Choose the right component for each input
- Provide helpful validation messages: Make error messages clear and actionable
- Set sensible defaults: Pre-fill fields when possible
- Handle loading states: Disable submit during async operations
- Preserve form state: Consider persisting to localStorage for long forms
- Test validation: Ensure all validation rules work as expected