|
| 1 | +## Validation component for material-ui forms |
| 2 | + |
| 3 | +Simple form validation component for material-ui library inspired by [formsy-react](https://github.com/christianalfoni/formsy-react) |
| 4 | + |
| 5 | +Supported types: |
| 6 | ++ Text |
| 7 | ++ Select |
| 8 | ++ AutoComplete |
| 9 | ++ Date |
| 10 | ++ Time |
| 11 | + |
| 12 | +Default validation rules: |
| 13 | ++ matchRegexp |
| 14 | ++ isEmail |
| 15 | ++ isEmpty |
| 16 | ++ required |
| 17 | ++ trim |
| 18 | ++ isNumber |
| 19 | ++ isFloat |
| 20 | ++ isPositive |
| 21 | + |
| 22 | + |
| 23 | +### Example |
| 24 | + |
| 25 | +[example.gif](../examples/example.gif) |
| 26 | + |
| 27 | +### Usage |
| 28 | + |
| 29 | +You can pass any props of field components except ``errorText``, use ``errorMessages`` instead. |
| 30 | +Your component must [provide a theme](http://www.material-ui.com/#/get-started/usage). |
| 31 | + |
| 32 | +````javascript |
| 33 | + |
| 34 | +import React from 'react'; |
| 35 | +import RaisedButton from 'material-ui/RaisedButton'; |
| 36 | +import { ValidatorForm, TextValidator} from 'react-material-ui-form-validator'; |
| 37 | + |
| 38 | +class MyForm extends React.Component { |
| 39 | + |
| 40 | + constructor(props) { |
| 41 | + super(props); |
| 42 | + |
| 43 | + this.handleChange = this.handleChange.bind(this); |
| 44 | + } |
| 45 | + |
| 46 | + handleChange(event) { |
| 47 | + const email = event.target.value; |
| 48 | + this.setState({ email }); |
| 49 | + } |
| 50 | + |
| 51 | + handleSubmit() { |
| 52 | + // your submit logic |
| 53 | + } |
| 54 | + |
| 55 | + render() { |
| 56 | + const { email } = this.state; |
| 57 | + return ( |
| 58 | + <ValidatorForm |
| 59 | + ref="form" |
| 60 | + onSubmit={this.handleSubmit} |
| 61 | + > |
| 62 | + <TextValidator |
| 63 | + floatingLabelText="Email" |
| 64 | + onChange={this.handleChange} |
| 65 | + name="email" |
| 66 | + value={email} |
| 67 | + validators={['required', 'isEmail']} |
| 68 | + errorMessages={['this field is required', 'email is not valid']} |
| 69 | + /> |
| 70 | + <RaisedButton type="submit" /> |
| 71 | + </ValidatorForm> |
| 72 | + ); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +```` |
| 77 | + |
| 78 | +You can add your custom rules: |
| 79 | +````javascript |
| 80 | + |
| 81 | +import React from 'react'; |
| 82 | +import RaisedButton from 'material-ui/RaisedButton'; |
| 83 | +import { ValidatorForm, TextValidator} from 'react-material-ui-form-validator'; |
| 84 | + |
| 85 | +class ResetPasswordForm extends React.Component { |
| 86 | + |
| 87 | + constructor(props) { |
| 88 | + super(props); |
| 89 | + this.state = { |
| 90 | + user: {}, |
| 91 | + }; |
| 92 | + this.handleChange = this.handleChange.bind(this); |
| 93 | + } |
| 94 | + |
| 95 | + componentWillMount() { |
| 96 | + // custom rule will have name 'isPasswordMatch' |
| 97 | + ValidatorForm.addValidationRule('isPasswordMatch', (value) => { |
| 98 | + if (value !== this.state.user.password) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + return true; |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + handleChange(event) { |
| 106 | + const { user } = this.state; |
| 107 | + user[event.target.name] = event.target.value; |
| 108 | + this.setState({ user }); |
| 109 | + } |
| 110 | + |
| 111 | + handleSubmit() { |
| 112 | + // your submit logic |
| 113 | + } |
| 114 | + |
| 115 | + render() { |
| 116 | + const { user } = this.state; |
| 117 | + |
| 118 | + return ( |
| 119 | + <ValidatorForm |
| 120 | + onSubmit={this.handleSubmit} |
| 121 | + > |
| 122 | + <TextValidator |
| 123 | + floatingLabelText="Password" |
| 124 | + onChange={this.handleChange} |
| 125 | + name="password" |
| 126 | + type="password" |
| 127 | + validators={['required']} |
| 128 | + errorMessages={['this field is required']} |
| 129 | + value={user.password} |
| 130 | + /> |
| 131 | + <TextValidator |
| 132 | + floatingLabelText="Repeat password" |
| 133 | + onChange={this.handleChange} |
| 134 | + name="repeatPassword" |
| 135 | + type="password" |
| 136 | + validators={['isPasswordMatch', 'required']} |
| 137 | + errorMessages={['password mismatch', 'this field is required']} |
| 138 | + value={user.repeatPassword} |
| 139 | + /> |
| 140 | + <RaisedButton type="submit" /> |
| 141 | + </ValidatorForm> |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | +```` |
| 146 | + |
| 147 | +Currently material-ui [doesn't support](https://github.com/callemall/material-ui/issues/3771) error messages for switches, but you can easily add your own: |
| 148 | +````javascript |
| 149 | +import React from 'react'; |
| 150 | +import Checkbox from 'material-ui/Checkbox'; |
| 151 | +import { ValidatorComponent } from 'react-material-ui-form-validator'; |
| 152 | +
|
| 153 | +export default class CustomCheckboxValidator extends ValidatorComponent { |
| 154 | +
|
| 155 | + render() { |
| 156 | + const { errorMessage, validators, requiredError, ...rest } = this.props; |
| 157 | + const { isValid } = this.state; |
| 158 | + const errorMessage = !isValid && this.getErrorMessage(); |
| 159 | + return ( |
| 160 | + <div> |
| 161 | + <Checkbox {...rest} /> |
| 162 | + {errorMessage} |
| 163 | + </div> |
| 164 | + ); |
| 165 | + } |
| 166 | +} |
| 167 | +
|
| 168 | +```` |
| 169 | +
|
| 170 | +### API |
| 171 | +
|
| 172 | +#### ValidatorForm |
| 173 | +
|
| 174 | +| Prop | Required | Type | Default value | Description | |
| 175 | +|-----------------|----------|----------|---------------|------------------------------------------------------------------------------------------------------------------------------| |
| 176 | +| onSubmit | true | function | | Callback for form that fires when all validations are passed | |
| 177 | +| instantValidate | false | bool | false | If true, form will be validated after each field change. If false, form will be validated only after clicking submit button. | |
| 178 | +
|
| 179 | +#### All validated fields (ValidatorComponent) |
| 180 | +
|
| 181 | +| Prop | Required | Type | Default value | Description | |
| 182 | +|-----------------|----------|----------|---------------|----------------------------------------------------------------------------------------| |
| 183 | +| validators | false | array | | Array of validators. See list of default validators above. | |
| 184 | +| errorMessages | false | array | | Array of error messages. Order of messages should be the same as `validators` prop. | |
| 185 | +| name | true | string | | Name of input | |
| 186 | +
|
| 187 | +### Contributing |
| 188 | +
|
| 189 | +This component covers all my needs, but feel free to contribute. |
0 commit comments