Socket
Socket
Sign inDemoInstall

mobx-form-model

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mobx-form-model

Minimal form controller model based on Mobx


Version published
Weekly downloads
4
increased by300%
Maintainers
1
Weekly downloads
 
Created
Source

Document is Working in progress. Welcome to fork :)

Minimal form model that supports to validating and dirty-checking based on Mobx.

Most features are copied from Angular forms

npm install -S mobx-form-model

Examples

// Simple Controller
import FormController from 'mobx-form-model';

const ctrl = new FormController('', [
  ({ value }) =>
    value == null || !value.length ? { required: 'value required' } : undefined
]);

ctrl.valid; // false
ctrl.errors.required; // value required

ctrl.update('hello');
ctrl.valid; // true
ctrl.errors; // undefined
ctrl.dirty; // true
// nested controllers
import FormController, {
  FormControllerGroup,
  FormControllerArray
} from 'mobx-form-model';

const form = new FormControllerGroup({
  name: new FormController('Tom'),
  firstStep: new FormControllerGroup({
    gender: new FormController(1),
    birthday: new FormController('1990-01-01')
  }),
  questioins: new FormControllerArray([
    new FormController(),
    new FormController()
  ])
});

Controller vs Group vs Array

Shared

Properties
  • value,当前的值
  • valid, true 表示当前没有错误
  • dirty, 表示值是否变更过。
  • errors, 所有validator执行玩合并后的结果
  • enabled, 表示当前controller是否有效。false表示当前controller不会影响parent controllervaliddirty值。

以上这些属性都是 observable

Methods
  • disable()
  • enable()
  • markAsPristine() 将dirty设置为false,比如表单提交之前先检查dirty,用户没修改表单就无需重复提交,那在表单提交后调用 markAsPristine() 就可以避免重复提交已经提交的表单。

Change Controler's value

update(newValue)更新 value
标记 dirty 为 true
校验数据
replace(newValue)更新 value
标记 dirty 为 true
校验数据
reset(newValue)更新 value
标记 dirty 为 false
校验数据

Keywords

FAQs

Package last updated on 07 May 2019

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