0.15.6 • Published 6 years ago

react-validate-framework v0.15.6

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

react-validate-framework

A lightweight and extensible React validation component

npm travis-ci Coverage Status npm

中文 README

Demo: https://minjieliu.github.io/react-validate-framework

You can check out the code to see examples.

How to use?

npm i react-validate-framework --save

Import:

import formConnect, {
  Checkbox,
  Radio,
  Select,
  Text,
  Textarea,
  Message,
} from 'react-validate-framework';

Rules and messages like this:

const schemas = {
  email: {
    rules: 'required | isEmail | maxLength(32) | validateFromServer',
    messages: 'Can not be empty! | Please enter a valid email address. | Can not exceed {{param}} characters. | | The email already exists.',
  },
  hobby: {
    rules: 'requiredField(phone) | selectLimit(2)',
    messages: 'email and hobby at least one entry! | Select at least {{param}}.',
  },
};

const methods = {
  selectLimit(field, param) {
    if (Array.isArray(field.value)) {
      return field.value.length >= param;
    }
    return false;
  },
  requiredField(field, param) {
    const otherField = this.fields[param];
    return this.required(field) || (otherField.result && this.required(otherField));
  },
  async validateFromServer(field, param) {
    return await doServerAPI();
  },
};

The BasicForm like this:

@formConnect(schemas, methods)
export default class BasicForm extends React.Component {
  
  static propTypes = {
    formControl: PropTypes.object,
  };

  constructor(props) {
    super(props);
    props.formControl.init({
      email: '',
      phone: '',
    }, {
      static: 'form-control',
      success: 'valid-success',
      error: 'valid-error',
    });
  }

  handleSubmit = async () => {
    const { formControl } = this.props;
    if (await formControl.validate()) {
      // Submit.
    }
  };

  render() {
    return (
      <div className="form-group">
        <Text
          name="email"
          placeholder="Please input your email"
          delay={100} // Asynchronous validation
        />
        <Message className="valid-error-message" name="email" />
        <Text name="phone" />
        <button onClick={this.handleSubmit}>提交</button>
      </div>
    );
  }
}

Validate methods can refer to validate-framework-utils

Form components

  • Checkbox
  • Radio
  • Select
  • Text
  • Textarea
  • Message

The name attribute is required in form components, delay debounce, Other parameters can be overridden.

Of course, you can also use unencapsulated form components, just specify value andonChange on the line:

const {
  formControl: {
    fields,
    onFormChange,
  },
} = this.props;

return (
  <input
    className={fields.email.className}
    name="email"
    type="text"
    onChange={onFormChange}
    value={fields.email.value}
    placeholder="Please input your email"
  />
);

API

Form params

nametypereturnsetStatedescription
fieldsObjectThe collection of fields
isAllValidBooleanGets the global validation status
formValuesObjectGets a list of form values
initfunctionthisInitializes the form value and classes
initClassNamesfunctionthisfalseInitializes classes
onFormChangefunctiontrueForm change event listener
changeValuesfunctionthistrueCustomize to change the values
validatefunctionPromise => BooleantrueValidate all fields
validateByNamesfunctionPromise => BooleantrueValidate the component through names
addValuesfunctionthistrueAdd one or more value
removeValuesfunctionthistrueRemove one or more value, If there is no name, it will all be removed
resetValuesfunctionthistrueReset one or more value, If there is no name, it will all be init
addSchemasfunctionthisfalseAdd one or more validation rules
removeSchemasfunctionthistrueRemove one or more validation rules, If there is no name, it will all be removed
formDidChangefunctionCallback

You can invoke the changeValues method to simulate a form change event.

Update log

0.15.x

  • Remove the delay parameter from FormControl.
  • Add the delay parameter to the props of the form component.
0.15.6

6 years ago

0.15.5

6 years ago

0.15.4

6 years ago

0.15.3

6 years ago

0.15.2

6 years ago

0.15.1

6 years ago

0.14.5

7 years ago

0.14.0

7 years ago

0.13.0

7 years ago

0.12.1

7 years ago

0.12.0

7 years ago

0.11.0

7 years ago

0.10.0

7 years ago

0.9.8

7 years ago

0.9.7

7 years ago

0.9.6

7 years ago

0.9.5

7 years ago

0.9.3

7 years ago

0.9.2

7 years ago

0.9.1

7 years ago

0.8.10

7 years ago

0.8.9

7 years ago

0.8.8

7 years ago

0.8.7

7 years ago

0.8.6

7 years ago

0.8.5

7 years ago

0.8.3

7 years ago

0.8.2

7 years ago

0.8.1

7 years ago

0.8.0

7 years ago

0.7.3

7 years ago

0.7.2

7 years ago

0.7.1

7 years ago

0.7.0

7 years ago

0.6.0

7 years ago

0.5.12

7 years ago

0.5.11

7 years ago

0.5.10

7 years ago

0.5.8

7 years ago

0.5.7

7 years ago

0.5.6

7 years ago

0.5.5

7 years ago

0.5.1

7 years ago

0.4.3

7 years ago

0.3.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.9

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.2

7 years ago

0.0.1

8 years ago