use-form
Usage
use-form provides bare minimum api to manage simple forms.
It includes handlers to set and validate values.
Hook does not depend on @mantine/core inputs and does not work with dom.
API
use-form hook accepts configuration object as single argument:
initialValues– object with initial form valuesvalidationRules– objects of functions that will be used to validate form valueserrorMessages– object of react nodes that will be used to ser errors, if not provided errors will be set to true
Hook returns object with properties:
values– current form valuessetValues– React useState hook setState action to setvaluessetFieldValue– function to set value of single fieldvalidate– function to validate allvalueswithvalidationRulesvalidateField– function to validate single field value withvalidationRuleserrors– object of booleans which contains results of runningvalidationRulesfunctions on corresponding fieldssetFieldError– function to set single field error inerrorsresetErrors– function to set allerrorsto nullsetErrors– React useState hook setState action to seterrorsreset– function to reset allvaluesanderrorsto initial stateonSubmit– wrapper function for form onSubmit event handlergetInputProps– returns an object withvalue,onChangeanderrorthat should be spread on input, work perfect with Mantine inputs
initialValues
initialValues is required for any form and defines form.values shape.
When form is reset with form.reset function these values are set to form.values.
validationRules
validationRules is an optional object of functions which are used to validate form.values.
If you do not need validation in your form, you can skip them:
validationRules must include only keys from initialValues,
keys from initialValues that do not have corresponding validation rule will always be considered valid.
errorMessages
You can provide errorMessages object that will be set to errors when validation fails:
form.values
values contains current values of form, it has the same shape as initialValues:
form.getInputProps
getInputProps function returns an object with value (of checked for checkboxes), onChange and error that should be spread on input, work perfect with Mantine inputs:
form.setFieldValue
setFieldValue function sets value at given key on values object:
Usually this function is used to work with input elements:
form.setValues
setValues allows you to set all values with single function call:
form.validate
validate function runs all validation rules on corresponding value's key, it returns true if all fields are valid:
form.validateField
validateField function allows you to run validations for individual fields,
for example, it can be useful if you want to validate field when it loses focus:
form.setFieldError
setFieldError allows you to bypass validation and manipulate errors object as you wish.
For example, you can remove error from field once it was focused or perform your own validation:
form.setErrors
setErrors sets errors object.
Use it when external fields validation occurs, e.g. on server:
form.resetErrors
resetErrors sets all errors to null:
form.reset
reset function sets all errors to null and sets values to initialValues:
form.onSubmit:
onSubmit takes function as an argument and calls it with values if form has no validation errors:
Examples
Validate field on blur
External field validation
Submit form with test@mantine.dev email to see external validation error:
Authentication form
TypeScript
Definition
Set values type
use-form will use values types from initialValues, but you can pass your own type:
Get form values type
Use typeof to get form values type: