
Product
Introducing Socket Firewall Enterprise: Flexible, Configurable Protection for Modern Package Ecosystems
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.
mobx-react-form
Advanced tools
A.K.A.
mobx-ajv-form(deprecated). Usemobx-react-forminstead.
See the Changelog or the Documentation for all the details.
npm install --save mobx-react-form
https://mobx-react-form-demo-zgltrpjjwi.now.sh/
const fields = {
username: {
label: 'Username',
value: 'SteveJobs'
},
email: {
label: 'Email',
value: 's.jobs@apple.com'
},
password: {
label: 'Password',
value: 'thinkdifferent'
}
};
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 };
Simply pass the fields and plugins objects to the constructor
import Form from 'mobx-react-form';
export default new Form({ fields, plugins });
Now define a rules property on the form fields as shown here.
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);
form.$('fieldkey')is a shortcut toform.fields.fieldkey
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 = (e) => {
e.preventDefault();
// clear the form
form.clear();
}
handleOnReset = (e) => {
e.preventDefault();
// reset to the default initial values
form.reset();
}
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!
FAQs
Reactive MobX Form State Management
We found that mobx-react-form demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Product
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.

Security News
Open source dashboard CNAPulse tracks CVE Numbering Authorities’ publishing activity, highlighting trends and transparency across the CVE ecosystem.

Product
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.