1.0.3 • Published 4 years ago

react-native-validation v1.0.3

Weekly downloads
15
License
Apache 2.0
Repository
github
Last release
4 years ago

react-native-form-validator

npm version

A simple validation library for react native

Installation

Run: $ npm i react-native-validation --save

Validators:

  • matchRegexp
  • isEmail
  • isEmpty
  • required
  • isNumber
  • isFloat
  • isPositive
  • maxNumber
  • minNumber
  • maxFloat
  • minFloat
  • isString
  • minStringLength
  • maxStringLength

Example Component:

<ValidationComponent
  component={
    <RkTextInput
      rkType="bordered"
      style={{ width: "100%" }}
      placeholder="You can type a description"
      value={description}
      onChangeText={value => this.setState({ description: value })}
    />
  }
  validators={["required","maxNumber:255"]}
  errorMessages={["this field is required", "must be max 255"]}
/>

Usage

import { ValidationForm, ValidationComponent } from "react-native-validator";

constructor(props, context) {
  super(props, context);
  ValidationComponent.setDefaultErrorMessageStyle({
    color: "white",
    fontSize: 12,
  });
}

render() {   
    return (
      <ValidationForm
        style={style.container}
        ref={ref => (this.form = ref)}
        onSubmit={() => this.props.saveUserList()}
        onError={() => console.log("houston we have a problem")}
      >
        <ValidationComponent
          component={
            <RkTextInput
              rkType="bordered"
              style={{ width: "100%" }}
              placeholder="List Name"
              value={name}
              onChangeText={value => this.setState({ name: value.trim() })}
            />
          }
          validators={["required", "isEmail"]}
          errorMessages={["this field is required", "email is not valid"]}
        />
        <ValidationComponent
          component={
            <TextInput
              style={{ width: "100%" }}
              placeholder="You can type a description"
              value={description}
              onChangeText={value => this.setState({ description: value })}
            />
          }
          errorMessageStyle={{
            color: "red"
          }}
          validators={["required"]}
          errorMessages={["this field is required"]}
        />
        <RkButton rkType="primary xlarge" onPress={() => this.form.validate()}>
          Next
        </RkButton>
      </ValidationForm>
    );
}
...

You can add your own rules

// validators={["isPasswordMatch:param1:param2"]}
ValidationForm.addValidationRule('isPasswordMatch', (value, param1, param2...) => {
    if (value !== this.state.user.password) {
        return false;
    }
    return true;
});

You can set default error style

constructor(props, context) {
  super(props, context);
  ValidationComponent.setDefaultErrorMessageStyle({
    color: "white",
  });
}

API

ValidationForm

  • Props
PropRequiredTypeDefault valueDescription
onSubmittruefunctiontriggered if form is valid
onErrorfalsefunctiontriggered if form is not valid
  • Methods
NameParamsReturnDescriptipon
validateCheck form is valid
isValidboolreturn current validation state

ValidationComponent

  • Props
PropRequiredTypeDefault valueDescription
validatorstruearrayArray of validators.
errorMessagestruearrayArray of error messages. Must be in the same order as validation
errorMessageStylefalseobjectError textComponent style
componenttrueobjectInput component(Must include value prop)
stylefalseobjectContainer style props
  • Methods
NameParamsReturnDescriptipon
setDefaultErrorMessageStylestyleObjectSet default style for error textComponent
1.0.3

4 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago