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-validator

v2.0.5

Published

Form validation for redux

Downloads

73

Readme

Redux Form Validation

An es6 redux form validator middleware that help you validate inputs.

Demo: Live Example | Source

Better Documentation: http://posabsolute.github.io/redux-flash-notification

Integration

1 npm install 'redux-form-validation' --save

2 Add the reducer to your root reducer


import {validateReducer} from 'redux-form-validation';

const rootReducer = combineReducers({
  validate: validateReducer,
});

export default rootReducer;

3 Add the validate middleware

import {validateMiddleware} from 'redux-form-validation';

const createStoreWithMiddleware = compose(
    validateMiddleware,
  ),
  window.devToolsExtension ? window.devToolsExtension() : f => f
)(createStore);

4 Connect Validate store, actions & specicify the component & model to validate. It is important this be available throughout every components that use validation, you can trickle them down throught props.

In your componentWillMount init the validation component:

import {validate, validateActions} from 'redux-form-validation';

const mapStateToProps = (state) => {
  return {
    validationStore: state.validation,
  };
};

const mapDispatchToProps = (
  dispatch,
) => {
  return {
    ...bindActionCreators(validateActions, dispatch),
  };
};


@connect(mapStateToProps, mapDispatchToProps)
export default class LoginComponent extends React.Component {
  componentWillMount() {
    this.validate = validate(this, userModel);
  }
  render() {
    return <LoginForm {...this.props} validate={this.validate} />;
  }
}

5. Add validation to your inputs, there is also a error label component for your convenience. Unfortunately you cannot use stateless component because the middleware makes use of refs.

  a. add {...validate} to your input
  b. add a name & ref to your input (must be the same)
  c. To get the error class, use className={validate.classes('input-line', 'url')}

It should look like:
```javascript
<input type="text" className={validate.classes('input-line', 'url')} ref="url" name="url" placeholder="Your Url" {...validate} />
<LabelError field={validate.fieldStore('url')} />
import React from 'react';
import LabelError from 'components/validation/labelErrorComponent';

class LoginFormComponent extends React.Component {
  render() {
    const {validate, onSubmit} = this.props;
    return (
      <form className="col-sm-6 col-lg-12 login-bottom-container" onSubmit={ (evt) => { evt.preventDefault(); onSubmit.call(this, validate);} }>
        <div className="form-group">
          <input type="text" className={validate.classes('input-line', 'url')} ref="url" name="url" placeholder="Jira Url (http://company.jira.net)" {...validate} />
          <LabelError field={validate.fieldStore('url')} />
        </div>
        <div className="form-group">
          <input type="text" className={validate.classes('input-line', 'username')} ref="username" name="username"  placeholder="Username" {...validate} />
          <LabelError field={validate.fieldStore('username')} />
        </div>
        <div className="form-group">
          <input type="password"  ref="password"  name="password" className={validate.classes('input-line', 'password')} placeholder="Password" {...validate} />
        </div>
        <div className="relative"><button type="submit" className="btn btn-default btn-full" >Sign in</button></div>
      </form>
    );
  }
}
  1. Create a model

Anatomy of a model

const userModel = {
  name:'userModel',
  data: {
    'url': {
      validate: {
        required: true,
        func: (value) => {
          return true;
        },
        message: 'This is a test',
      },
    },
  },
}

7 Using webpack? include jsx/es6

  module: {
    loaders: [{
      test:[/\.jsx$/,  /\.js$/],
      loaders: ['react-hot', 'babel?stage=0&loose[]=es6.modules'],
      include: [
        path.resolve(__dirname, "src"),
        path.resolve(__dirname, "node_modules/flash-notification-react-redux")
      ],
    }, {
      test: [/\.scss$/, /\.css$/],
      loader: 'css?localIdentName=[path]!postcss-loader!sass',
    }],
  },
};

8 You're done.

Using actions

You can use validation actions to execute code depending if the form or input is valid. It's a good way to control side effects like calling an api action once the field if valid.

Validate Sync Form

onSubmit: function(validateProps) {
  const inputs = this.refs;
  if (validateProps.form(form)) {
    // form is valid, redirect to nre page
  }else{
    // form is not valid
  }
}

Validate Async Form

If you validate asyncly 1 input or form, you must use a promise instead of just using a bool.

onSubmit: function submit(validateProps) {
  const inputs = this.refs;
  validateProps.form(inputs).then(() =>{
    console.log('form is valid');
  }).catch(() => { 
    console.log("form is not valid"); 
  });
}

Validate Sync input

if(this.validate.input(value, field)){
  // input is valid
}else{
  // input is not valid
}

Validate Async input

this.validate.input(value, field).then(() => {
  // input is valid
})
.catch(function(errorMessage) {
  // input is not valid
});

Validation model

data

A Model must have a data object that describe fields to validate. Under the validate object list all the validators you want to use.

Global Validate functions

The model can also have global validation functions that are executed once all inputs are valid.

validate(form, dispatch)

Used to do sync validations after all your inputs are valid. Must return true or false

const userModel = {
  name:'userModel',
  data: {
    'url': {
      validate: {
        required: true,
        func: (value) => {
          return true;
        },
        message: 'This is a test',
      },
    },
  },
  validate: (form, dispatch) => {
    // form
    let validate = false;
    if (!form.url.value) {
      dispatch({
        type: 'GROWLER__SHOW',
        growler: {
          text: 'Please enter your url',
          type: 'growler--error',
        },
      });
      validate = false;
    }

    return true;
  },
};

Built-in validators

func validator

Lets you implement a custom function used for validation.

const userModel = {
  name:'userModel',
  data: {
    'username': {
      validate: {
        required: true,
        pattern: 'email',
        async: function() {
          setTimeout( () => {
              this.resolve("yes");
          }, 5000);
        },
      },
    },

async validator

Lets you implement a custom async function used for validation using a Promise. Must return this.resolve or this.reject. You can reject with a custom message passed as a string.

const userModel = {
  name:'userModel',
  data: {
    'username': {
      validate: {
        required: true,
        pattern: 'email',
        async: function() {
          setTimeout( () => {
              this.reject("Sorry, this username is already used.");
          }, 5000);
        },
      },
    },

required

Validates if the attribute is required or not. This can be specified as either a boolean value or a function that returns a boolean value.

const userModel = {
  name:'userModel',
  data: {
    'username': {
      validate: {
        required: true,
        pattern: 'email',
      },
    },
  },
};

acceptance

Validates that something has to be accepted, e.g. terms of use. true or 'true' are valid.

const userModel = {
  name:'userModel',
  data: {
    'username': {
      validate: {
        required: true,
      acceptance: true
    }
  }
};

min

Validates that the value has to be a number and equal to or more than the min value specified.

const userModel = {
  name:'userModel',
  data: {
    'age': {
      validate: {
        min: 1,
      }
    }
  }
});

max

Validates that the value has to be a number and equal to or less than the max value specified.

const userModel = {
  name:'userModel',
  data: {
    'age': {
      validate: {
        max: 100,
      }
    }
  }
};

range

Validates that the value has to be a number and equal to or between the two numbers specified.

const userModel = {
  name:'userModel',
  data: {
    'age': {
      validate: {
        range: [1, 10],
      }
    }
  }
};

length

Validates that the value has to be a string with length equal to the length value specified.

const userModel = {
  name:'userModel',
  data: {
    'postalCode': {
      validate: {
        length: 4,
      }
    }
  }
};

minLength

Validates that the value has to be a string with length equal to or greater than the min length value specified.

const userModel = {
  name:'userModel',
  data: {
    'password': {
      validate: {
         minLength: 8
      }
    }
  }
};

maxLength

Validates that the value has to be a string with length equal to or less than the max length value specified.

const userModel = {
  name:'userModel',
  data: {
    'password': {
      validate: {
        maxLength: 100
      }
    }
  }
};

rangeLength

Validates that the value has to be a string and equal to or between the two numbers specified.

const userModel = {
  name:'userModel',
  data: {
    'password': {
      validate: {
        rangeLength: [6, 100]
      }
    }
  }
};

oneOf

Validates that the value has to be equal to one of the elements in the specified array. Case sensitive matching.

const userModel = {
  name:'userModel',
  data: {
    'country': {
      validate: {
        oneOf: ['Norway', 'Sweeden']
      }
    }
  }
};

equalTo

Validates that the value has to be equal to the value of the attribute with the name specified.

const userModel = {
  name:'userModel',
  data: {
    'password': {
      validate: {
        equalTo: 'password'
      }
    }
  }
};

pattern

Validates that the value has to match the pattern specified. Can be a regular expression or the name of one of the built in patterns.

const userModel = {
  name:'userModel',
  data: {
    'email': {
      validate: {
        pattern: 'email'
      }
    }
  }
};

The built-in patterns are:

  • number - Matches any number (e.g. -100.000,00)
  • email - Matches a valid email address (e.g. [email protected])
  • url - Matches any valid url (e.g. http://www.example.com)
  • digits - Matches any digit(s) (i.e. 0-9)

Specify any regular expression you like:

const userModel = {
  name:'userModel',
  data: {
    'email': {
      validate: {
        pattern: /^sample/
      }
    }
  }
};

Limitations

This component is based on the use of redux, react, es6 & es7 (decorators) and webpack for loading the css as an import module.