All Projects → NewOldMax → React Material Ui Form Validator

NewOldMax / React Material Ui Form Validator

Licence: mit
Simple validator for forms designed with material-ui components.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Material Ui Form Validator

react-declarative
A React form builder which interacts with a JSON endpoint to generate nested 12-column grids with input fields and automatic state management in a declarative style. Endpoint is typed by TypeScript guards (IntelliSense available). This tool is based on material-ui components, so your application will look beautiful on any device...
Stars: ✭ 17 (-94.12%)
Mutual labels:  material-ui, form-validation
Material Singleinputform
A single EditText instead of a classical form. Library that implements flavienlaurent's singleinputform
Stars: ✭ 202 (-30.1%)
Mutual labels:  material-ui, form-validation
Materiallettericon
Material first letter icon like lollipop contacts icon. Letter(s) on a shape drawn on canvas.
Stars: ✭ 255 (-11.76%)
Mutual labels:  material-ui
Rubik
Material Design 风格的 Vue.js UI 组件库
Stars: ✭ 277 (-4.15%)
Mutual labels:  material-ui
Validify
Simple-as-possible React form validation
Stars: ✭ 271 (-6.23%)
Mutual labels:  form-validation
React Reactive Form
Angular like reactive forms in React.
Stars: ✭ 259 (-10.38%)
Mutual labels:  form-validation
Phrase
Clojure(Script) library for phrasing spec problems.
Stars: ✭ 275 (-4.84%)
Mutual labels:  form-validation
soft-ui-dashboard-react
Soft UI Dashboard React - Free Dashboard using React and Material UI
Stars: ✭ 122 (-57.79%)
Mutual labels:  material-ui
Syncano Dashboard
The Syncano Dashboard built with React.
Stars: ✭ 287 (-0.69%)
Mutual labels:  material-ui
Carbon
Material Design implementation for Android 4.0+. Shadows, ripples, vectors, fonts, animations, widgets, rounded corners and more.
Stars: ✭ 2,942 (+917.99%)
Mutual labels:  material-ui
Material Table
Datatable for React based on material-ui's table with additional features
Stars: ✭ 3,198 (+1006.57%)
Mutual labels:  material-ui
Sequent
A simple continuous animation library for Android UI.
Stars: ✭ 263 (-9%)
Mutual labels:  material-ui
Material Ui Superselectfield
multiselection autocomplete dropdown component for Material-UI
Stars: ✭ 260 (-10.03%)
Mutual labels:  material-ui
Laravel Recaptcha
Google ReCaptcha package for Laravel
Stars: ✭ 273 (-5.54%)
Mutual labels:  form-validation
Create React App Material Typescript Redux
A ready to use boilerplate for starting big react projects
Stars: ✭ 257 (-11.07%)
Mutual labels:  material-ui
Mdi Material Ui
Material-UI SvgIcon components for Material Design Icons.
Stars: ✭ 276 (-4.5%)
Mutual labels:  material-ui
react-movies-finder
React Movies finder is a React app to search movies and series using redux, redux-thunk, React Hooks, and Material UI
Stars: ✭ 27 (-90.66%)
Mutual labels:  material-ui
Formvuelar
Vue form components with server-side validation in mind
Stars: ✭ 263 (-9%)
Mutual labels:  form-validation
Material Kit React
React Dashboard made with Material UI’s components. Our pro template contains features like TypeScript version, authentication system with Firebase and Auth0 plus many other
Stars: ✭ 3,465 (+1098.96%)
Mutual labels:  material-ui
Googlekeepclone
A clone of Google Keep with its original Material Design aesthetics
Stars: ✭ 281 (-2.77%)
Mutual labels:  material-ui

Validation component for material-ui forms

license npm version Build Status

Demo

Installation

npm install react-material-ui-form-validator

Versions

  • 0.x, 1.x - supports material-ui <= 0.x
  • ^2.0.0 - supports material-ui >= 1.x || 3.x || 4.x

Implementation of react-form-validator-core for material-ui

Migration guide

From <2.1.1 to >=2.1.1

Breaking changes was introduced to react-form-validator-core in order to avoid legacy context. If you have custom validator components then you should change render method of your input components to renderValidatorComponent.

Before:

import React from 'react';
import { ValidatorComponent } from 'react-material-ui-form-validator';

class SomeValidator extends ValidatorComponent {
    render() {
        // return your validated component
    }
}

export default TextValidator;

After:

import React from 'react';
import { ValidatorComponent } from 'react-material-ui-form-validator';

class SomeValidator extends ValidatorComponent {
    renderValidatorComponent() {
        // return your validated component
    }
}

export default TextValidator;

Info

Supported types:

Some rules can accept extra parameter, example:

<TextValidator
   {...someProps}
   validators={['minNumber:0', 'maxNumber:255', 'matchRegexp:^[0-9]$']}
/>

Example

Usage

You can pass any props of field components, but note that errorText prop will be replaced when validation errors occurred. Your component must provide a theme.

import React from 'react';
import Button from '@material-ui/core/Button';
import { ValidatorForm, TextValidator} from 'react-material-ui-form-validator';

class MyForm extends React.Component {

    state = {
        email: '',
    }

    handleChange = (event) => {
        const email = event.target.value;
        this.setState({ email });
    }

    handleSubmit = () => {
        // your submit logic
    }

    render() {
        const { email } = this.state;
        return (
            <ValidatorForm
                ref="form"
                onSubmit={this.handleSubmit}
                onError={errors => console.log(errors)}
            >
                <TextValidator
                    label="Email"
                    onChange={this.handleChange}
                    name="email"
                    value={email}
                    validators={['required', 'isEmail']}
                    errorMessages={['this field is required', 'email is not valid']}
                />
                <Button type="submit">Submit</Button>
            </ValidatorForm>
        );
    }
}

You can add your custom rules:

import React from 'react';
import Button from '@material-ui/core/Button';
import { ValidatorForm, TextValidator} from 'react-material-ui-form-validator';

class ResetPasswordForm extends React.Component {

    state = {
        user: {
            password: '',
            repeatPassword: '',
        },
    };

    componentDidMount() {
        // custom rule will have name 'isPasswordMatch'
        ValidatorForm.addValidationRule('isPasswordMatch', (value) => {
            if (value !== this.state.user.password) {
                return false;
            }
            return true;
        });
    }

    componentWillUnmount() {
        // remove rule when it is not needed
        ValidatorForm.removeValidationRule('isPasswordMatch');
    }

    handleChange = (event) => {
        const { user } = this.state;
        user[event.target.name] = event.target.value;
        this.setState({ user });
    }

    handleSubmit = () => {
        // your submit logic
    }

    render() {
        const { user } = this.state;

        return (
            <ValidatorForm
                onSubmit={this.handleSubmit}
            >
                <TextValidator
                    label="Password"
                    onChange={this.handleChange}
                    name="password"
                    type="password"
                    validators={['required']}
                    errorMessages={['this field is required']}
                    value={user.password}
                />
                <TextValidator
                    label="Repeat password"
                    onChange={this.handleChange}
                    name="repeatPassword"
                    type="password"
                    validators={['isPasswordMatch', 'required']}
                    errorMessages={['password mismatch', 'this field is required']}
                    value={user.repeatPassword}
                />
                <Button type="submit">Submit</Button>
            </ValidatorForm>
        );
    }

Currently material-ui doesn't support error messages for switches, but you can easily add your own:

import React from 'react';
import red from '@material-ui/core/colors/red';
import Checkbox from '@material-ui/core/Checkbox';
import { ValidatorComponent } from 'react-material-ui-form-validator';

const red300 = red['500'];

const style = {
    right: 0,
    fontSize: '12px',
    color: red300,
    position: 'absolute',
    marginTop: '-25px',
};

class CheckboxValidatorElement extends ValidatorComponent {

    renderValidatorComponent() {
        const { errorMessages, validators, requiredError, value, ...rest } = this.props;

        return (
            <div>
                <Checkbox
                    {...rest}
                    ref={(r) => { this.input = r; }}
                />
                {this.errorText()}
            </div>
        );
    }

    errorText() {
        const { isValid } = this.state;

        if (isValid) {
            return null;
        }

        return (
            <div style={style}>
                {this.getErrorMessage()}
            </div>
        );
    }
}

export default CheckboxValidatorElement;
    componentDidMount() {
        ValidatorForm.addValidationRule('isTruthy', value => value);
    }
...
    <CheckboxValidatorElement
        ...
        validators=['isTruthy']
        errorMessages=['this field is required']
        checked={value}
        value={value} // <---- you must provide this prop, it will be used only for validation
    />
Advanced usage

Contributing

This component covers all my needs, but feel free to contribute.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].