use-form
Installation
@mantine/form package does not depend on any other libraries, you can use it with or without @mantine/core inputs:
Install with npm:
Install with yarn:
Usage
API
useForm hook accepts a single argument with a configuration object that includes the following properties:
initialValues– initial form values, form types are generated based on this valuevalidate(optional) – an object with validation rules or a validation function that receives form values as an argument and returns object with validation errorsschema(optional) – zod, joi or yup validation schema, if providedvalidateis ignoredinitialErrors(optional) – initial form errors, object of React nodes
Hook returns an object with the following properties:
values– current form valuessetValues– handler to set all form valuessetFieldValue– handler to set value of the specified form fielderrors– current validation errorssetErrors– sets validation errorsclearErrors– clears all validation errorsclearFieldError– clears validation error of the specified fieldsetFieldError– sets validation error of the specified fieldsetListItem– sets list item at specified field and indexremoveListItem– removes list item at specified field and indexaddListItem– appends list item to the end of the list at specified fieldreorderListItem– reorders list item with given position at specified fieldvalidate– validates all fields, returns validation resultsvalidateField– validates specified field, returns validation resultsonSubmit– wrapper function for form onSubmit event handlerreset– resetsvaluesto initial state, clears all validation errorsgetInputProps– returns an object with value, onChange and error that should be spread on inputgetListInputProps– returns an object with value, onChange and error that should be spread on input that is used to manage list value
Set form values
setFieldValue handler
setFieldValue handler sets value of the specified field. When this handler is called field validation error is automatically cleared.
setFieldValue can be used with all @mantine/core inputs, example with TextInput component:
setValues handler
setValues handler allows to set all form values with one function:
reset handler
reset handler clears all validation errors and resets all values to initialValues:
Validation
useForm hook supports three validation strategies: rules object, validation function and schema validation (covered in separate guide).
Validation with rules object
To validate form with rules object, provide an object of functions which take field value as an argument and return error message (any React node) or null if field is valid:
You can also get all form values as a second argument to perform field validation based on other fields values. For example, this strategy can be used to validate that password confirmation matches with password:
Validation function
Another approach to handle validation is to provide a function to validate.
Function takes form values as single argument and should return object that contains
errors of corresponding fields. If field is valid or field validation is not required, you can either return null or simply omit it
from the validation results.
Schema validation
useForm hook also supports schema validation with several popular libraries zod, joi and yup.
See separate guide to learn how to setup schema validation.
validate handler
validate handler can be used to validate all fields all at once. It sets errors and returns an object with validation results:
validateField handler
validateField handler can be used to validate single field. It sets field error in errors and returns validation results:
Errors
useForm hook gives you full control of errors state:
You can use all of the handlers shown above to create custom validation logic, for example, you can set errors received from backend.
Initial errors
You can set initial errors with initialErrors property:
Errors type
You can use any React node as an error message:
Note that errors that have null or undefined will be automatically removed:
getInputProps handler
getInputProps handler is a primary way to add value, error and onChange props
to Mantine inputs.
onSubmit handler
onSubmit handler is a wrapper function that will:
- prevent default form event
- run
form.validatewhen form is submitted - call provided function with form values when form is submitted if all fields are valid
TypeScript
Get form values type
Set form values type
By default, form values types will be inferred from initialValues.
To avoid that, you can pass type to useForm hook, this approach is useful when
types cannot be inferred or when you want to provide more specific types: