mantine-form-rules
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

Mantine form rules

npm

Rules collection to work with @mantine/form.

demo

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'),
    ]),
  },
});

Package Sidebar

Install

npm i mantine-form-rules

Weekly Downloads

1

Version

1.1.1

License

none

Unpacked Size

28.3 kB

Total Files

9

Last publish

Collaborators

  • davigmacode