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

redux-form-validation

v1.0.0-rc1

Published

An helper to make redux form validation easyer

Downloads

31

Readme

redux-form-validation

Build Status npm version npm downloads

Installation

npm install --save redux-form-validation

How to use

adds a helper to display and validate fields for redux-form

builtin validation can be found in /src/basic-validation.js

How to add your validation:


  import FormMessages from 'redux-form-validation';
  
  FormMessages.addValidation('required', function (field, value, prop, dispatch, allValues, allProps) {
     return prop ? !value : false
   })

to make async validation you can return a promise in your validation function

NOTE: all the validations are indexed by the key, if you add a require validation it will overwrite the validation used before The validation function can return a message or true if the field is invalid If the field is valid the validation function must return false

How to display error messages in form

Component Props:

  • tagName: Specify element type to render as message list container (default is div)
  • errorCount : Specify how many errors to be displayed at once (default -1= all)
  • Meta : The redux-form Meta (or for other uses a object with {touch, error})

example for how to use validator:

  import React, {Component} from 'react';
  import {reduxForm, Field} from 'redux-form';
  import {connect} from 'react-redux';
  import FormMessages from 'redux-form-validation';
  import {generateValidation} from 'redux-form-validation';
  
  const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
  
  
  const validations = {
    email: {
      validateOnBlur: true,
      required: true,
      minLength: 5,
      email: true,
      promise: function (fieldName, fieldValue, dispatch) {
        return sleep(1000).then(() => {
          if (['[email protected]', '[email protected]'].indexOf(fieldValue.trim()) > -1) {
            return Promise.reject('That email ' + fieldValue + ' is taken')
          }
        })
      }
    },
    retryEmail: {
      validateOnBlur: true,
      required: true,
      matchField: 'email',
    },
    name: {
      required: true
    },
    subject: {
      required: true,
      minLength: 5
    },
    message: {
      required: true,
      minLength: 10
    }
  };
  
  
  const submit = (values, dispatch) => {
    console.log('sending mail to contact', values);
  };
  
  @connect()
  @reduxForm({
    form: 'contact',
    ...generateValidation(validations)
  })
  export default class ContactForm extends Component {
    // probably you will want to use different messages for different fields but for the demo this is good enough
    renderField = ({input, label, type, meta}) => {
      return (
        <div>
          <label>{label}</label>
          <div>
            <input {...input} placeholder={label} type={type} />
            <FormMessages tagName="ul" meta={meta}>
              <li when="promise">
                {meta && meta.error && meta.error.promise}
              </li>
              <li when="matchField">
                the retry email must be the same as the email
              </li>
              <li when="required">
                this field is required
              </li>
              <li when="email">
                please insert a valid email
              </li>
              <li when="minLength">
                this field must have at least 5 characters
              </li>
            </FormMessages>
          </div>
        </div>
      );
    }
  
    render() {
      const {
        handleSubmit,
        submitting,
        valid,
        pristine,
        asyncValidating,
      } = this.props;
      console.log('this.props', this.props);
      var submitLabel = "Send";
  
      if (pristine) {
        submitLabel = "Fill in your message";
      } else if (asyncValidating) {
        submitLabel = "Validating...";
      } else if (submitting) {
        submitLabel = "Sending...";
      } else if (!valid) {
        submitLabel = "Please fill all fields correctly";
      }
      return (
        <form onSubmit={handleSubmit(submit)}>
          <Field
            name="email"
            type="email"
            component={this.renderField}
            label="Username ([email protected] is taken)"
          />
  
          <Field
            name="retryEmail"
            type="email"
            component={this.renderField}
            label="Retry email"
          />
          <Field
            name="name"
            type="text"
            component={this.renderField}
            label="Username"
          />
  
          <Field
            name="subject"
            type="text"
            component={this.renderField}
            label="Subject"
          />
          <Field
            name="message"
            type="text"
            component={this.renderField}
            label="Message"
          />
          <button onClick={handleSubmit(submit)}>
            {submitLabel}
          </button>
        </form>
      );
    }
  }

Without ES2015

var temp = generateValidation(validations);
reduxForm({
     form: 'contact',
     asyncValidate: temp.asyncValidate,
     asyncBlurFields: temp.asyncBlurFields,
     fields: temp.fields,
   })(YourComponent)

Examples:

to run the example project you need to clone the repo and run npm i -d && npm start