6.10.1 • Published 4 months ago

mobx-react-form v6.10.1

Weekly downloads
10,406
License
MIT
Repository
github
Last release
4 months ago

Documentation Live Demo Demo Code Tutorial Join Discord Channel

MobX React Form

Reactive MobX Form State Management

NPM

GitHub Workflow Status (with branch) GitHub release (latest by date) npm bundle size Codecov Coverage node GitHub license GitHub closed pull requests GitHub closed issues Downloads Downloads Backers on Open Collective Sponsors on Open Collective

Features

Quick Start

Edit form-quickstart

npm install --save mobx-react-form

Choose and Setup a Validation Plugin

Below we are creating a plugins object using the validatorjs package to enable DVR functionalities (Declarative Validation Rules).

import dvr from 'mobx-react-form/lib/validators/DVR';
import validatorjs from 'validatorjs';

const plugins = {
  dvr: dvr({
    package: validatorjs
  })
};

See Validation Plugins for more info on supported packages.

Define the Form Fields

Define the fields as a collection with a rules property for the validation.

const fields = [{
  name: 'email',
  label: 'Email',
  placeholder: 'Insert Email',
  rules: 'required|email|string|between:5,25',
}, {
  name: 'password',
  label: 'Password',
  placeholder: 'Insert Password',
  rules: 'required|string|between:5,25',
  type: 'password',
}, {
  name: 'passwordConfirm',
  label: 'Password Confirmation',
  placeholder: 'Confirm Password',
  rules: 'required|string|same:password',
  type: 'password',
}];

See Fields Definitions and all available Field Props on the docs.

Define the Validation Hooks

const hooks = {
  onSuccess(form) {
    alert('Form is valid! Send the request here.');
    // get field values
    console.log('Form Values!', form.values());
  },
  onError(form) {
    alert('Form has errors!');
    // get all form errors
    console.log('All form errors', form.errors());
  }
}

See more on the docs about the Validation Hooks and the Event Hooks

Initialize the Form

Simply pass the fields, plugins and hooks objects to the constructor

import MobxReactForm from 'mobx-react-form';

const myForm = new MobxReactForm({ fields }, { plugins, hooks });

Learn more on the docs about the Form Instance and the Form Options

Pass the myForm to a react component

The package provide some built-in and ready to use Event Handlers:

onSubmit(e), onClear(e), onReset(e) & more...

import React from 'react';
import { observer } from 'mobx-react';

export default observer(({ myForm }) => (
  <form onSubmit={myForm.onSubmit}>
    <label htmlFor={myForm.$('email').id}>
      {myForm.$('email').label}
    </label>
    <input {...myForm.$('email').bind()} />
    <p>{myForm.$('email').error}</p>

    {/* ... other inputs ... */}

    <button type="submit" onClick={myForm.onSubmit}>Submit</button>
    <button type="button" onClick={myForm.onClear}>Clear</button>
    <button type="button" onClick={myForm.onReset}>Reset</button>

    <p>{myForm.error}</p>
  </form>
));

See more on the docs about the Field Props Bindings

Extending the Form class

See how to implement the same configuration of this quickstart extending the Form class

Contributing

  1. Fork the repository
  2. Make applicable changes (with tests!)
  3. To run tests: npm run test
  4. Ensure builds succeed: npm run build
  5. Commit and run pre-commit checks: npm run commit

New Issues

When opening new issues, provide the setup of your form in a CodeSandbox.

These issues, and the ones which provides also PR with failing tests will get higher priority.

Contributors

This project exists thanks to all the people who contribute.

Backers

Thank you to all our backers! 🙏 [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

6.10.1

4 months ago

6.10.0

4 months ago

6.9.4

9 months ago

6.9.0

10 months ago

6.9.2

10 months ago

6.9.1

10 months ago

6.9.3

10 months ago

6.8.3

10 months ago

6.7.5

10 months ago

6.8.1

10 months ago

6.8.0

10 months ago

6.8.2

10 months ago

6.7.2

11 months ago

6.7.1

11 months ago

6.7.4

11 months ago

6.7.3

11 months ago

6.7.0

11 months ago

6.6.1

11 months ago

6.5.0

1 year ago

6.6.0

12 months ago

6.4.0

1 year ago

6.3.7

1 year ago

6.3.4

1 year ago

6.3.3

1 year ago

6.3.6

1 year ago

6.3.5

1 year ago

6.3.2

1 year ago

6.1.0

2 years ago

6.1.1

2 years ago

6.2.1

2 years ago

6.2.0

2 years ago

6.2.3

2 years ago

6.2.2

2 years ago

6.3.0

2 years ago

6.3.1

2 years ago

5.8.0

2 years ago

5.9.1

2 years ago

5.9.0

2 years ago

6.0.0

2 years ago

5.10.2

2 years ago

5.10.1

2 years ago

5.10.0

2 years ago

5.4.1

2 years ago

5.4.0

2 years ago

5.5.2

2 years ago

5.5.1

2 years ago

5.5.0

2 years ago

5.6.0

2 years ago

5.7.1

2 years ago

5.7.0

2 years ago

5.3.2

2 years ago

5.3.1

2 years ago

5.0.0

3 years ago

5.1.0

3 years ago

5.2.0

3 years ago

5.3.0

3 years ago

4.0.0

3 years ago

3.2.0

3 years ago

3.1.2

3 years ago

3.1.1

3 years ago

3.1.0

4 years ago

3.0.0

4 years ago

2.0.9

4 years ago

2.0.8

5 years ago

2.0.7

5 years ago

2.0.6

5 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.39.1

6 years ago

1.39.0

6 years ago

1.38.0

6 years ago

1.37.0

6 years ago

1.36.3

6 years ago

1.36.2

6 years ago

1.36.1

6 years ago

1.36.0

6 years ago

1.35.3

6 years ago

1.35.2

6 years ago

1.35.1

6 years ago

1.35.0

6 years ago

1.34.0

7 years ago

1.33.0

7 years ago

1.32.3

7 years ago

1.32.2

7 years ago

1.32.1

7 years ago

1.32.0

7 years ago

1.31.23

7 years ago

1.31.22

7 years ago

1.31.21

7 years ago

1.31.20

7 years ago

1.31.19

7 years ago

1.31.18

7 years ago

1.31.17

7 years ago

1.31.16

7 years ago

1.31.15

7 years ago

1.31.14

7 years ago

1.31.13

7 years ago

1.31.12

7 years ago

1.31.11

8 years ago

1.31.10

8 years ago

1.31.9

8 years ago

1.31.8

8 years ago

1.31.7

8 years ago

1.31.6

8 years ago

1.31.5

8 years ago

1.31.4

8 years ago

1.31.3

8 years ago

1.31.2

8 years ago

1.31.1

8 years ago

1.31.0

8 years ago

1.30.0

8 years ago

1.29.0

8 years ago

1.28.0

8 years ago

1.27.1

8 years ago

1.27.0

8 years ago

1.26.0

8 years ago

1.25.0

8 years ago

1.24.1

8 years ago

1.24.0

8 years ago

1.23.2

8 years ago

1.23.1

8 years ago

1.23.0

8 years ago

1.22.2

8 years ago

1.22.1

8 years ago

1.22.0

8 years ago

1.21.0

8 years ago

1.20.5

8 years ago

1.20.4

8 years ago

1.20.3

8 years ago

1.20.2

8 years ago

1.20.1

8 years ago

1.20.0

8 years ago

1.19.2

8 years ago

1.19.1

8 years ago

1.19.0

8 years ago

1.18.18

8 years ago

1.18.17

8 years ago

1.18.16

8 years ago

1.18.15

8 years ago

1.18.14

8 years ago

1.18.13

8 years ago

1.18.12

8 years ago

1.18.11

8 years ago

1.18.10

8 years ago

1.18.9

8 years ago

1.18.8

8 years ago

1.18.7

8 years ago

1.18.6

8 years ago

1.18.5

8 years ago

1.18.4

8 years ago

1.18.3

8 years ago

1.18.2

8 years ago

1.18.1

8 years ago

1.18.0

8 years ago

1.17.10

8 years ago

1.17.9

8 years ago

1.17.8

8 years ago

1.17.7

8 years ago

1.17.6

8 years ago

1.17.5

8 years ago

1.17.4

8 years ago

1.17.3

8 years ago

1.17.2

8 years ago

1.17.1

8 years ago

1.17.0

8 years ago

1.16.18

8 years ago

1.16.17

8 years ago

1.16.16

8 years ago

1.16.15

8 years ago

1.16.14

8 years ago

1.16.13

8 years ago

1.16.12

8 years ago

1.16.11

8 years ago

1.16.10

8 years ago

1.16.9

8 years ago

1.16.8

8 years ago

1.16.7

8 years ago

1.16.6

8 years ago

1.16.5

8 years ago

1.16.4

8 years ago

1.16.3

8 years ago

1.16.2

8 years ago

1.16.1

8 years ago

1.16.0

8 years ago

1.15.0

8 years ago

1.14.5

8 years ago

1.14.4

8 years ago

1.14.3

8 years ago

1.14.2

8 years ago

1.14.1

8 years ago

1.14.0

8 years ago

1.13.28

8 years ago

1.13.27

8 years ago

1.13.26

8 years ago

1.13.25

8 years ago

1.13.24

8 years ago

1.13.23

8 years ago

1.13.22

8 years ago

1.13.21

8 years ago

1.13.20

8 years ago

1.13.19

8 years ago

1.13.18

8 years ago

1.13.17

8 years ago

1.13.16

8 years ago

1.13.15

8 years ago

1.13.14

8 years ago

1.13.13

8 years ago

1.13.12

8 years ago

1.13.11

8 years ago

1.13.10

8 years ago

1.13.9

8 years ago

1.13.8

8 years ago

1.13.7

8 years ago

1.13.6

8 years ago

1.13.5

8 years ago

1.13.4

8 years ago

1.13.3

8 years ago

1.13.2

8 years ago

1.13.1

8 years ago

1.13.0

8 years ago

1.12.1

8 years ago

1.12.0

8 years ago

1.11.13

8 years ago

1.11.12

8 years ago

1.11.11

8 years ago

1.11.10

8 years ago

1.11.9

8 years ago

1.11.8

8 years ago

1.11.7

8 years ago

1.11.6

8 years ago

1.11.5

8 years ago

1.11.4

8 years ago

1.11.3

8 years ago

1.11.2

8 years ago

1.11.1

8 years ago

1.11.0

8 years ago

1.10.4

8 years ago

1.10.3

8 years ago

1.10.2

8 years ago

1.10.1

8 years ago

1.10.0

8 years ago

1.9.2

8 years ago

1.9.1

8 years ago

1.9.0

8 years ago

1.8.0

8 years ago

0.0.1

8 years ago