npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-validate-form

v1.1.3

Published

Form validation for React Native

Downloads

47

Readme

react-native-validate-form

A simple form validation for React Native

Getting Started

NPM

Install react-native-validate-form using NPM with:

npm install --save react-native-validate-form

Import

import { Form, Field } from 'react-native-validate-form';

Usage

See github documentation for more info.

import React from 'react';
import { View, Text } from 'react-native';
import { Form, Field } from 'react-native-validate-form';

import InputField from './InputField';

const required = value => (value ? undefined : 'This is a required field.');
const email = value => value && !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,5}$/i.test(value) ? 'Please provide a valid email address.' : undefined;

class MyForm extends React.Component {
  constructor() {
    super();

    this.state = {
      errors: [],
      email: ''
    }
  }
  submitForm() {
    let submitResults = this.myForm.validate();

    let errors = [];

    submitResults.forEach(item => {
      errors.push({ field: item.fieldName, error: item.error });
    });

    this.setState({ errors: errors });
  }

  render() {
    return(
      <Form
        ref={(ref) => this.myForm = ref}
        validate={true}
        submit={this.submitForm.bind(this)}
        errors={this.state.errors}
      >
        <Field
          required
          component={InputField}
          validations={[ required, email ]}
          name="email"
          value={this.state.email}
          onChangeText={(val) => this.setState({ email: val })}
          customStyle={{ width: 100 }}
        />
      </Form>
    );
  }
}
  • InputField will represent your input field component, this component will receive the errors that will be thrown by this.myForm.validate().
  import React from 'react';
  import { TextInput, Text, View } from 'react-native';

  const InputField = ({
    name,           // field name - required
    customStyle,
    onChangeText,   // event
    value,          // field value
    disabled,
    placeholder,
    errors,         // this array prop is automatically passed down to this component from <Form />
  }) => {
    return (
      <View>
        <TextInput
          value={value && value}
          onChangeText={onChangeText ? (val) => onChangeText(val) : null}
          placeholder={placeholder ? placeholder : ""}
          disabled={disabled}
          style={customStyle ? customStyle : {}}
        />

        { errors && errors.length > 0 && errors.map((item, index) =>
            item.field === name && item.error ?
              <Text style={{ color: 'red' }}>
                {item.error}
              </Text>
            : <View />
          )
        }
      </View>
    );
  }

  export default InputField;
  • If you have nested <Field /> components, you need to explicitly pass down errors as props so you can display the errors accordingly.
  • There's no need to pass down errors as props if your <Field /> component is a direct child of <Form />.
  // ...
    <Form
      ref={(ref) => this.myForm = ref}
      validate={true}
      submit={this.submitForm.bind(this)}
      errors={this.state.errors} // you still need to pass errors as props to Form
    >
      <Field
        required
        component={InputField}
        validations={[ required, email ]}
        name="email"
        value={this.state.email}
        onChangeText={(val) => this.setState({ email: val })}
        customStyle={{ width: 100 }}
        // no need to pass down errors as props if <Field /> is a direct child of <Form />
      />
      <View>
        <Field
          required
          component={InputField}
          validations={[ required, email ]}
          name="email"
          value={this.state.email}
          onChangeText={(val) => this.setState({ email: val })}
          customStyle={{ width: 100 }}
          errors={this.state.errors} // explicitly pass down errors as props if your <Field /> is inside an element
        />
      </View>
    </Form>
  // ...

TODO: make an example in the repo for better documentation

Options

You can pass these props to the Form and Field components:

<Form
  ref={(ref) => this.myForm = ref}
  validate={true}
  submit={onSubmit}
>
  <Field
    required
    component={InputField}
    validations={[ sampleValidation() ]}
  />
</Form>

Props you can pass for the <Form /> component

|Name |Type |Default |Required |Description | |-----------------------|-------------------------|----------------------|---------------|-------------------------------------------------------------------------------------| |ref |string |null |yes |reference name | |validate |boolean |false |no |set this to true to enable validation | |submit |function |() => null |no |function callback if form is valid | |failed |function |() => null |no |function callback if form is invalid | |errors |array |[] |no |array that contains all your field errors and messages | |style |object |{} |no |style object |

Props you can pass for the <Field /> component

|Name |Type |Default |Required |Description | |-----------------------|-------------------------|----------------------|---------------|-------------------------------------------------------------------------------------| |required |boolean |false |no |set this to true to require the field | |component |component / func |null |yes |input component or any component that needs validation | |validateFieldName |string |'value' |no |name of the prop that will be validated | |validations |func / array |[] |no |function or array of functions that contains your validation | |customStyle |object |{} |no |style object |

Credits

Jefferson Aux

License

The MIT License

Copyright (c) 2018 Jefferson Aux