New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

react-validation-provider

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-validation-provider

React validation component

  • 0.1.12
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
21
decreased by-58%
Maintainers
1
Weekly downloads
 
Created
Source

React Validation Provider

Non-intrusive validation library for React.

Installation

npm install --save react-validation-provider

Example usage

The first you need to define in you application is the form element types that is going to be validated. This is done by wrapping the input elements/components inside a react component that is decorated with @validationComponent decorator.

import { validate } from 'react-validation-provider';

@validate()
export default class TextInput extends React.Component {
    render() {
        return ( 
            <input type="text" {...this.props} />
        );
    }
}
//alternative without using decorator
import { validate } from 'react-validation-provider';

class TextInput extends React.Component {
    render() {
        return ( 
            <input type="text" {...this.props} />
        );
    }
}

export default validate()(TextInput);

Afterwards you simply place these components inside your form components

    <label>Name</label>
    <TextInput value={this.state.name}
               onChange={(ev) => this.setState({name: ev.target.value})}
               rules={[required]}
    <label>Year</label>
    <TextInput value={this.state.year}
               onChange={(ev) => this.setState({year: ev.target.value})}
               rules={[required, year]}

The rules prop is a special prop used by the

component to validate the value prop of the wrapped component. The rules are simply implemented by defining and object with a validation expression and a message hint method.

export const required = (message = "Required field") => {
    return {
        validate: value => {
            return !!value.trim();
        },
        hint: () => {
            return message;
        }
    };
};

The last thing you have to define the the validation scope. This is typically the top most form component that contains all the input components that are going to be evaulted. The result of the evaluated validation is injected into the isValid prop.

import { scope } from 'react-validation-provider';

@scope()
export default class MyForm extends React.Component {
   render() {
       return( 
           <div>
               //form elements here

               <button disabled={!this.props.isValid} onClick={() => this.submitMyForm()}>Submit</button>
           </div>
       );

   }
}

Keywords

FAQs

Package last updated on 07 Mar 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc