Form & Validation
useValidation
Input validation with custom rules, real-time feedback, and error messages.
import { useValidation } from "saha-ui"Uses
General validation utilities for form values with rules (required, min/max length, pattern, custom) and helpers to validate single fields or the full form.
Props
| Name | Type | Description | Default |
|---|---|---|---|
| rules | Record<string, any> | Object mapping field names to rule sets (required, minLength, maxLength, pattern, custom). | - |
| options | object | Behavior options such as validation mode (e.g. onChange or onBlur). | - |
Returns
| Name | Description |
|---|---|
| errors | Current error map keyed by field name. |
| touched | Map of touched fields. |
| validate(values) | Validate all fields and return { isValid, errors } while updating internal state. |
| validateSingle(name, value) | Validate a single field by name and value. |
| clearErrors / clearError(name) | Clear all errors or a specific field error. |
| setTouchedField(name, touched) | Mark a field as touched/untouched. |
Example
const { errors, validate } = useValidation({ email: { required: true, pattern: { value: /@/, message: 'Invalid' } } }); const result = validate({ email: 'a' });Notes
Returns error messages as strings; supports custom validators returning boolean or message.