# mantine-form-rules

> Rules collection to work with @mantine/form

Latest version **1.1.1** (published 2023-08-07) · 0 weekly downloads

## Install

```sh
npm install mantine-form-rules
pnpm add mantine-form-rules
yarn add mantine-form-rules
bun add mantine-form-rules
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2023-08-07 |
| First published | 2023-08-04 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 27.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Irfan Vigma Taufik |
| Maintainers | davigmacode |
| Keywords | react, next, nextjs, ui, components, ui-kit, library, frontend, form, form-rules, form-validation |

## Links

- npm: https://www.npmjs.com/package/mantine-form-rules
- Repository: https://github.com/davigmacode/mantine-plugins
- Homepage: https://github.com/davigmacode/mantine-plugins#readme
- Issues: https://github.com/davigmacode/mantine-plugins/issues
- npm.io page: https://npm.io/package/mantine-form-rules

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.1.1 (latest) — 2023-08-07
- 1.1.0 — 2023-08-05
- 1.0.0 — 2023-08-04

## README

# Mantine form rules

[![npm](https://img.shields.io/npm/dm/mantine-form-rules
)](https://www.npmjs.com/package/mantine-form-rules)

Rules collection to work with @mantine/form.

[demo](https://mantine-plugins.vercel.app/mantine-form-rules)

## Installation

```bash
# With yarn
yarn add mantine-form-rules @mantine/form

# With npm
npm install mantine-form-rules @mantine/form
```

## Quick Usage
```ts
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
```ts
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'),
    ]),
  },
});
```

---
_Source: https://npm.io/package/mantine-form-rules · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
