Socket
Book a DemoInstallSign in
Socket

react-form-validation-hoc

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-form-validation-hoc

Form validation for React

latest
Source
npmnpm
Version
1.1.9
Version published
Maintainers
1
Created
Source

react-form-validation

Form validation for React

Install

$ tnpm install react-form-validation-hoc --save

Usage

Please refer to async-validator for verification rules

Example

import React from 'react';
import create from 'react-form-validation-hoc';

class MyForm extends React.Component {
  state = {
    name: '',
    sex: '',
    school: '',
  }

  handleSubmit = (event) => {
    event.preventDefault();
    this.props.validator.validateFields((errors, values) => {
      console.log(errors); // eg. { name: "please enter your name", data.sex: "Please fill in male or female", data.school: "school is required" }
      console.log(values); // eg. { name: "tony", data: { sex: "male", school: "ABC School" } }
    });
  }

  render() {
    const { FieldDecorator, errors } = this.props.validator;
    return (
      <form onSubmit={this.handleSubmit}>
        <div>
          <FieldDecorator
            name={'name'}
            value={this.state.name}
            rules={[
              { required: true, message: 'please enter your name' },
            ]}
          />
          name:
          <input
            value={this.state.name}
            onChange={(event) => {
              this.setState({
                name: event.target.value,
              });
            }}
          />
        </div>
        <div>name error: <span style={{ color: 'red' }}>{errors.name}</span></div>

        <div>
          <FieldDecorator
            name={'data.sex'}
            value={this.state.sex}
            rules={[
              {
                validator(rule, value, callback) {
                  if (value === 'male' || value === 'female') {
                    callback();
                  } else {
                    callback('Please fill in male or female');
                  }
                },
              },
            ]}
          />
          sex:
          <input
            value={this.state.sex}
            onChange={(event) => {
              this.setState({
                sex: event.target.value,
              });
            }}
          />
        </div>
        <div>sex error: <span style={{ color: 'red' }}>{errors['data.sex']}</span></div>

        <div>
          <FieldDecorator
            name={'data.school'}
            value={this.state.school}
            rules={[
              { required: true, message: 'school is required' },
            ]}
          />
          School:
          <input
            value={this.state.school}
            onChange={(event) => {
              this.setState({
                school: event.target.value,
              });
            }}
          />
        </div>
        <div>school error: <span style={{ color: 'red' }}>{errors['data.school']}</span></div>

        <input type="submit" value="Submit" />
      </form>
    );
  }
}

export default create()(MyForm);

Keywords

react

FAQs

Package last updated on 11 Feb 2020

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.