Form validation

Validate fields with use-form hook

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:

import { useForm } from '@mantine/form';
import { NumberInput, TextInput, Button } from '@mantine/core';
function Demo() {
const form = useForm({
initialValues: { name: '', email: '', age: 0 },
// functions will be used to validate values at corresponding key
validate: {
name: (value) => (value.length < 2 ? 'Name must have at least 2 letters' : null),
email: (value) => (/^\S+@\S+$/.test(value) ? null : 'Invalid email'),
age: (value) => (value < 18 ? 'You must be at least 18 to register' : null),
},
});
return (
<form onSubmit={form.onSubmit(console.log)}>
<TextInput label="Name" placeholder="Name" {...form.getInputProps('name')} />
<TextInput mt="sm" label="Email" placeholder="Email" {...form.getInputProps('email')} />
<NumberInput
mt="sm"
label="Age"
placeholder="Age"
min={0}
max={99}
{...form.getInputProps('age')}
/>
<Button type="submit" mt="sm">
Submit
</Button>
</form>
);
}

Rule function arguments

Each form rule receives the following arguments:

  • value – value of field
  • values – all form values
  • path – field path, for example user.email or cart.0.price

path argument can be used to get information about field location relative to other fields, for example you can get index of array element:

import { useForm } from '@mantine/form';
const form = useForm({
initialValues: { a: [{ b: 1 }, { b: 2 }] },
validate: {
a: {
b: (value, values, path) => (path === 'a.0.b' ? 'error' : null),
},
},
});

Validation based on other form values

You can get all form values as a second rule function argument to perform field validation based on other form values. For example, you can validate that password confirmation is the same as password:

import { useForm } from '@mantine/form';
import { PasswordInput, Group, Button, Box } from '@mantine/core';
function Demo() {
const form = useForm({
initialValues: {
password: 'secret',
confirmPassword: 'sevret',
},
validate: {
confirmPassword: (value, values) =>
value !== values.password ? 'Passwords did not match' : null,
},
});
return (
<Box sx={{ maxWidth: 340 }} mx="auto">
<form onSubmit={form.onSubmit((values) => console.log(values))}>
<PasswordInput
label="Password"
placeholder="Password"
{...form.getInputProps('password')}
/>
<PasswordInput
mt="sm"
label="Confirm password"
placeholder="Confirm password"
{...form.getInputProps('confirmPassword')}
/>
<Group position="right" mt="md">
<Button type="submit">Submit</Button>
</Group>
</form>
</Box>
);
}

Function based validation

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.

import { useForm } from '@mantine/form';
import { Box, TextInput, NumberInput, Button, Group } from '@mantine/core';
function Demo() {
const form = useForm<{ name: string; age: number | undefined }>({
initialValues: { name: '', age: undefined },
validate: (values) => ({
name: values.name.length < 2 ? 'Too short name' : null,
age:
values.age === undefined
? 'Age is required'
: values.age < 18
? 'You must be at least 18'
: null,
}),
});
return (
<Box sx={{ maxWidth: 340 }} mx="auto">
<form onSubmit={form.onSubmit((values) => console.log(values))}>
<TextInput label="Name" placeholder="Name" {...form.getInputProps('name')} />
<NumberInput mt="sm" label="Age" placeholder="You age" {...form.getInputProps('age')} />
<Group position="right" mt="md">
<Button type="submit">Submit</Button>
</Group>
</form>
</Box>
);
}

Schema based validation

Out of the box @mantine/form supports schema validation with:

You will need to install one of the libraries yourself, as @mantine/form package does not depend on any of them. If you do not know what validation library to choose, we recommend choosing zod as the most modern and developer-friendly library.

zod

joi

yup

superstruct

Validate fields on change

To validate all fields on change set validateInputOnChange option to true:

const form = useForm({ validateInputOnChange: true });

You can also provide an array of fields paths to validate only those values:

import { FORM_INDEX, useForm } from '@mantine/form';
const form = useForm({ validateInputOnChange: ['name', 'email', `jobs.${FORM_INDEX}.title`] });

Validate fields on blur

To validate all fields on blur set validateInputOnBlur option to true:

const form = useForm({ validateInputOnBlur: true });

You can also provide an array of fields paths to validate only those values:

import { FORM_INDEX, useForm } from '@mantine/form';
const form = useForm({ validateInputOnBlur: ['name', 'email', `jobs.${FORM_INDEX}.title`] });

Clear field error on change

By default, field error is cleared when value changes. To change this, set clearInputErrorOnChange to false:

const form = useForm({ clearInputErrorOnChange: false });

Validation in onSubmit handler

form.onSubmit accepts two arguments: first argument is handleSubmit function that will be called with form values, when validation was completed without errors, second argument is handleErrors function, it is called with errors object when validation was completed with errors.

You can use handleErrors function to perform certain actions when user tries to submit form without values, for example, you can show a notification:

import { useForm } from '@mantine/form';
import { TextInput, Button } from '@mantine/core';
import { showNotification } from '@mantine/notifications';
function Demo() {
const form = useForm({
initialValues: { name: '', email: '' },
validate: {
name: (value) => (value.length < 2 ? 'Name must have at least 2 letters' : null),
email: (value) => (/^\S+@\S+$/.test(value) ? null : 'Invalid email'),
},
});
const handleError = (errors: typeof form.errors) => {
if (errors.name) {
showNotification({ message: 'Please fill name field', color: 'red' });
} else if (errors.email) {
showNotification({ message: 'Please provide a valid email', color: 'red' });
}
};
const handleSubmit = (values: typeof form.values) => {};
return (
<form onSubmit={form.onSubmit(handleSubmit, handleError)}>
<TextInput label="Name" placeholder="Name" {...form.getInputProps('name')} />
<TextInput mt="sm" label="Email" placeholder="Email" {...form.getInputProps('email')} />
<Button type="submit" mt="sm">
Submit
</Button>
</form>
);
}

isValid handler

form.isValid performs form validation with given validation functions, rules object or schema, but unlike form.validate it does not set form.errors and just returns boolean value that indicates whether form is valid.

import { useForm } from '@mantine/form';
const form = useForm({
initialValues: { name: '', age: 0 },
validate: {
name: (value) => (value.trim().length < 2 ? 'Too short' : null),
age: (value) => (value < 18 ? 'Too young' : null),
},
});
// get validation status of all values
form.isValid(); // -> false
// get validation status of field
form.isValid('name'); // -> false