
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
mobx-react-form
Advanced tools
Automagically manage React forms state with MobX and automatic validation.
A.K.A.
mobx-ajv-form
(deprecated). Usemobx-react-form
instead.
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.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.