New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mobx-react-form

Package Overview
Dependencies
Maintainers
1
Versions
249
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mobx-react-form

Automagically manage React forms state with MobX and automatic validation.

  • 1.9.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
9K
decreased by-27.02%
Maintainers
1
Weekly downloads
 
Created
Source

MobX React Form

A.K.A. mobx-ajv-form (deprecated). Use mobx-react-form instead.

Automagically manage React forms state with MobX and automatic validation.

Travis Build Codecov Coverage Downloads npm node GitHub license

NPM



Changelog & Documentation

See the Changelog or the Documentation for all the details.

Install

npm install --save mobx-react-form

Demo (mobx-ajv-form)

http://www.webpackbin.com/VJZUlhWc-

Features

  • Automatic Reactive Form State Management with MobX Magic
  • Automatic Reactive Validation & Error Messages
  • Validation Plugins & Multiple Validation Styles
  • Support for Sync & Async Validation functions
  • Support for Material UI, React Widgets

Usage

Define the form fields

Optionally using a default property, it will fill the field on reset instead of using the initial value.

const fields = {
  username: {
    label: 'Username',
    value: 'SteveJobs'
    default: '',
  },
  email: {
    label: 'Email',
    value: 's.jobs@apple.com'
  },
  password: {
    label: 'Password',
    value: 'thinkdifferent'
  }
};

Choose and Setup a Validation Plugin

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

See Validation Plugins and Supported Validation Packages for more info.

import validatorjs from 'validatorjs';

const plugins = { dvr: validatorjs };
Create the form

Simply pass the fields and plugins objects to the constructor

import Form from 'mobx-react-form';

...

export default new Form({ fields, plugins });

Pass the form to a react component:

form.$('fieldkey') is a shortcut to form.fields.fieldkey

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

const FormComponent = ({ form, events }) => (
  <form>
    <input
      type="text"
      name={form.$('username').name}
      value={form.$('username').value}
      placeholder={form.$('username').label}
      onChange={form.$('username').sync}
    />
    <p>{form.$('username').error}</p>

    ...

    <button
      type="submit"
      disabled={!form.isValid}
      onClick={events.handleOnSubmit}
    >Submit</button>

    <button
      type="submit"
      onClick={events.handleOnReset}
      >Reset</button>

    <button
      type="submit"
      onClick={events.handleOnClear}
      >Clear</button>

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

export default observer(FormComponent);

Deal with events:

handleOnSubmit
  handleOnSubmit = (e) => {
    e.preventDefault();

    form.validate()
      .then((isValid) => isValid
        ? onSuccess()
        : onError());
  };

  onError() {
    // get all form errors
    console.log('All form errors', form.errors());
    // invalidate the form with a custom error message
    form.invalidate('This is a generic error message!');
  }

  onSuccess() {
    alert('Form is valid! Send the request here.');
    // get field values
    console.log('Form Values!', form.values());
  }
handleOnClear
const handleOnClear = (e) => {
  e.preventDefault();

  // clear the form
  form.clear();
}
handleOnReset
const handleOnReset = (e) => {
  e.preventDefault();

  // reset to the default initial values
  form.reset();
}

Documentation Index

Plugins Extensions

Contributing

If you want to contribute to the development, do not hesitate to fork the repo and send pull requests.

And don't forget to star the repo, I will ensure more frequent updates! Thanks!

Keywords

FAQs

Package last updated on 05 Sep 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc