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

@feerzlay/react-form-validation

v0.7.0

Published

Yet another form validation library for React ಠ_ಠ

Downloads

19

Readme

React Form Validation

Table of Contents

Features

  • Simple usage with help of higher-order component.
  • You define your own validation rules.
  • Support for both synchronous and asynchronous validations.
  • Automatically validates every input inside a form. Even the ones nested into another component.
  • Easy integration with UI frameworks.

Installation

npm

npm install feerzlay/react-form-validation --save

yarn

yarn add feerzlay/react-form-validation

Usage

Create a validator

This library does not contain any built-in validation rules. So before creating a validator you need to define you own rules. A rule is a function of following signature - (name, value, values, ..args). This function should return null on validation pass, and error message on validation fail. You can also return a Promise which resolves to null or String.

So, you can define your rules like this:

const rules = {
  required: (name, value) => {
    if (value.length > 0) return null;
    return '${name} can\'t be empty';
  },
  min: (name, value, values, min) => {
    if (value.length >= min) return null;
    return `${name} must be at least ${min} characters long`;
  }
};

Note that values agrument contains values of every validatable input inside a form. You can use it to define rules like a password confirmation.

Now you can import a validator creator and pass previously defined rules as an agrument:

import { createValidator } from '@feerzlay/react-form-validation';
const validator = createValidator(rules);

Create a higher-order component

This step is pretty simple. Just import a HOC creator and previously created validator as an agrument.

import { createValidatable } from '@feerzlay/react-form-validation';
const Validatable = createValidatable(validator);

Wrap your inputs

Wrap your inputs with created higher-order component.

const BasicInput = props => <input {...props} />;
const Input = Validatable(BasicInput);

Take a note that HOC expects input to recieve name and value props and emit onChange and onBlur values.

Create a Form component

For a basic usage you only need to import a form creator function and call it.

import { createForm } from '@feerzlay/react-form-validation';
const Form = createForm();

This function also accepts an object of options. For now this object have only one entry:

{ event: 'onChange' }

You can override it to be onBlur or just an empty string. If onBlur is used, then only form submit and input blur will trigger validation. If empty string is used, then only form submit will trigger validation.

Created component can be used almost as a regular <form> tag, but with two differences:

  • onSubmit event triggered only on successful validation.
  • It has an additional onErrorsChange event, which you should use to store information about errors and display them to the user.

Done :tada:

Now when you have form and input components you are ready to start building your forms.

<Form onSubmit={...} onErrorsChange={...}>
  <Input
    value={...} onChange={...} name="username"
    validate="required"
  />
  <Input
    value={...} onChange={...} name="password"
    validate="required|min:8"
  />
  <Input
    value={...} onChange={...} name="optional"
  />
</Form>

We use validate prop do describe which rules must be used for input validation. Individual rules should be separated by |. To pass agruments use : and then write a list of arguments separated by ,.

You can also manually add and remove errors from inputs by calling addError({ name, error }) and removeError(name) methods on a form component.

See example for more usage details.

License

MIT - Denis Yakshov - 2018