Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mobx-input

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mobx-input

Form library for MobX, with observable state and validations

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
Maintainers
1
Weekly downloads
 
Created
Source

Form validation for MobX and react-bootstrap.

Taken strong inspiration from: react-bootstrap-validation

Example Form

import React from 'react'
import { Button } from 'react-bootstrap'
import { ValidatedInput, submit } from 'mobx-input'

export class UserRegisterComponent extends React.Component {
  onSubmit = () => {
    const result = submit(this.props.controller.formData)
    if (result.valid) {
      this.props.controller.registerUser(result.values)
    }
  }

  render() {
    const {appState, controller} = this.props
    const job = appState.currentJob

    return (
      <div className='container'>
        <ValidatedInput
          type='text'
          label='First Name'
          name='firstName'
          validate='required'
          model={controller.formData}
          errorHelp={{
            required: 'First Name is required'
          }}
          />
        <ValidatedInput
          type='text'
          label='Last Name'
          name='lastName'
          validate='required'
          model={controller.formData}
          errorHelp={{
            required: 'Last Name is required'
          }}
          />
        <ValidatedInput
          type='text'
          label='Email'
          name='emailAddress'
          validate='required,isEmail'
          model={controller.formData}
          errorHelp={{
            isEmail: 'Email is invalid',
            required: 'Email is required'
          }}
          />
        <ValidatedInput
          type='password'
          label='Password'
          name='password'
          validate='required,isLength:4:60'
          model={controller.formData}
          errorHelp={{
            isLength: 'Password must be at least 4 characters long',
            required: 'Password is required'
          }}
          />
        <Button
          type='submit'
          bsSize='large'
          onClick={this.onSubmit}
          bsStyle='primary'> Register
        </Button>
      </div>
    )
  }
}

Just provide any MobX observable object as model to all form fields. All values and validation data will be stored there. Pass this object to submit function to trigger validations for untouched components. Validation rules are specified in validate attribute, and error messages in errorHelp

ValidatedInput

An extension of react-bootstrap's Input component. Should be used instead of the original one for all the fields that need to be validated. All ValidatedInputs should have name property defined.

Properties

name: String required

This property is inherited from Input with only difference that it is required for ValidatedInput.

<ValidatedInput
    name='email'
    validate='required,isEmail'
/>
validate: String

Validation rule is a combination of validator.js method names separated with comma.

<ValidatedInput
    name='email'
    validate='required,isEmail,isLength:5:60'
/>

In the example above, input's value will be validated with three methods. isLength method also receives additional params. Inverse rules (like !isNull) are supported, although in errorHelp object they're looked up without the exclamation mark.

errorHelp: Object|String

Can be either a string with error text or an object with map ruleName => errorText.

<ValidatedInput
    name="email"
    validate='required,isEmail',
    errorHelp={{
        required: 'Please enter your email',
        isEmail: 'Invalid email'
    }}
/>
model: Object

Any MobX observable object to store all form data

... More docs soon ...

Keywords

FAQs

Package last updated on 16 Aug 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