mantine-form-rules
v1.1.1
Published
Rules collection to work with @mantine/form
Downloads
44
Maintainers
Readme
Mantine form rules
Rules collection to work with @mantine/form.
Installation
# With yarn
yarn add mantine-form-rules @mantine/form
# With npm
npm install mantine-form-rules @mantine/form
Quick Usage
import { PasswordInput, Box, Button } from '@mantine/core';
import { useForm } from '@mantine/form';
import rules from 'mantine-form-rules';
export default function Page() {
const form = useForm({
validateInputOnChange: true,
validateInputOnBlur: true,
initialValues: {
currentPassword: '',
newPassword: '',
newPasswordConfirm: '',
},
validate: {
currentPassword: rules.isRequired(),
newPassword: rules([
rules.isRequired(),
rules.hasUpperCase(),
rules.hasNumber(),
rules.minLength(6),
], { accumulate: true }), // return every or first occurred error
newPasswordConfirm: rules([
rules.isRequired(),
rules.matchesField('newPassword', 'Passwords are not the same'),
]),
},
});
return (
<Box w={300} mx="auto" mt="xl">
<form onSubmit={form.onSubmit((values) => console.log(values))}>
<PasswordInput
label="Current Password"
placeholder="Your password"
{...form.getInputProps('currentPassword')}
/>
<PasswordInput
label="New Password"
placeholder="Your password"
mt="sm"
{...form.getInputProps('newPassword')}
/>
<PasswordInput
label="Confirm New Password"
placeholder="Your password"
mt="sm"
{...form.getInputProps('newPasswordConfirm')}
/>
<Button type="submit" mt="lg">Submit</Button>
</form>
</Box>
);
}
Tree Shaking
import {
some, // combine rule and return the first occurred error
every, // combine rule and return every occurred error
isRequired,
hasUpperCase,
hasNumber,
minLength,
matchesField,
} from 'mantine-form-rules';
const form = useForm({
initialValues: {
password: '',
passwordConfirm: '',
},
validate: {
password: every([
isRequired(),
hasUpperCase(),
hasNumber(),
minLength(6),
]),
passwordConfirm: some([
isRequired(),
matchesField('newPassword', 'Passwords are not the same'),
]),
},
});