Skip to content

Commit 655ae7d

Browse files
m.volkovm.volkov
authored andcommitted
Moved to prop-types packages. Ability to handle errorText. Readme updated.
1 parent 2e2682c commit 655ae7d

9 files changed

+80
-38
lines changed

Readme.md

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
Simple form validation component for material-ui library inspired by [formsy-react](https://github.com/christianalfoni/formsy-react)
88

99
Supported types:
10-
+ Text
11-
+ Select
12-
+ AutoComplete
13-
+ Date
14-
+ Time
10+
+ Text ([TextValidator](https://github.com/NewOldMax/react-material-ui-form-validator/blob/master/src/TextValidator.jsx))
11+
+ Select ([SelectValidator](https://github.com/NewOldMax/react-material-ui-form-validator/blob/master/src/SelectValidator.jsx))
12+
+ AutoComplete ([AutoCompleteValidator](https://github.com/NewOldMax/react-material-ui-form-validator/blob/master/src/AutoCompleteValidator.jsx))
13+
+ Date ([DateValidator](https://github.com/NewOldMax/react-material-ui-form-validator/blob/master/src/DateValidator.jsx))
14+
+ Time ([TimeValidator](https://github.com/NewOldMax/react-material-ui-form-validator/blob/master/src/TimeValidator.jsx))
1515

1616
Default validation rules:
1717
+ matchRegexp
@@ -30,7 +30,7 @@ Default validation rules:
3030

3131
### Usage
3232

33-
You can pass any props of field components except ``errorText``, use ``errorMessages`` instead.
33+
You can pass any props of field components, but note that ``errorText`` prop will be replaced when validation errors occurred.
3434
Your component must [provide a theme](http://www.material-ui.com/#/get-started/usage).
3535

3636
````javascript
@@ -151,24 +151,63 @@ class ResetPasswordForm extends React.Component {
151151
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:
152152
````javascript
153153
import React from 'react';
154+
import { red300 } from 'material-ui/styles/colors';
154155
import Checkbox from 'material-ui/Checkbox';
155156
import { ValidatorComponent } from 'react-material-ui-form-validator';
156157
157-
export default class CustomCheckboxValidator extends ValidatorComponent {
158+
class CheckboxValidatorElement extends ValidatorComponent {
158159
159160
render() {
160-
const { errorMessages, validators, requiredError, ...rest } = this.props;
161-
const { isValid } = this.state;
162-
const errorMessage = !isValid && this.getErrorMessage();
161+
const { errorMessages, validators, requiredError, value, ...rest } = this.props;
162+
163163
return (
164164
<div>
165-
<Checkbox {...rest} />
166-
{errorMessage}
165+
<Checkbox
166+
{...rest}
167+
ref={(r) => { this.input = r; }}
168+
/>
169+
{this.errorText()}
170+
</div>
171+
);
172+
}
173+
174+
errorText() {
175+
const { isValid } = this.state;
176+
177+
if (isValid) {
178+
return null;
179+
}
180+
181+
const style = {
182+
right: 0,
183+
fontSize: '12px',
184+
color: red300,
185+
position: 'absolute',
186+
marginTop: '-25px',
187+
};
188+
189+
return (
190+
<div style={style}>
191+
{this.getErrorMessage()}
167192
</div>
168193
);
169194
}
170195
}
171196
197+
export default CheckboxValidatorElement;
198+
````
199+
````javascript
200+
componentWillMount() {
201+
ValidatorForm.addValidationRule('isTruthy', value => value);
202+
}
203+
...
204+
<CheckboxValidatorElement
205+
...
206+
validators=['isTruthy']
207+
errorMessages=['this field is required']
208+
checked={value}
209+
value={value} <---- you must provide this prop, it will be used only for validation
210+
/>
172211
````
173212
174213
##### [Advanced usage](https://github.com/NewOldMax/react-material-ui-form-validator/wiki)

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-material-ui-form-validator",
3-
"version": "0.2.1",
3+
"version": "0.3.0",
44
"description": "Simple validator for forms designed with material-ui components.",
55
"main": "./lib/index.js",
66
"scripts": {
@@ -13,7 +13,7 @@
1313
"url": "git+https://github.com/NewOldMax/react-material-ui-form-validator.git"
1414
},
1515
"keywords": [
16-
"materia-ui",
16+
"material-ui",
1717
"form",
1818
"validation"
1919
],
@@ -26,7 +26,8 @@
2626
"peerDependencies": {
2727
"material-ui": ">=0.16.0 <1",
2828
"react": "^15.0.0",
29-
"react-dom": "^15.0.0"
29+
"react-dom": "^15.0.0",
30+
"prop-types": "^15.0.0"
3031
},
3132
"devDependencies": {
3233
"babelify": "7.3.0",

src/AutoCompleteValidator.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ export default class AutoCompleteValidator extends ValidatorComponent {
88

99
render() {
1010
// eslint-disable-next-line
11-
const { errorMessages, validators, requiredError, ...rest } = this.props;
11+
const { errorMessages, validators, requiredError, errorText, ...rest } = this.props;
1212
const { isValid } = this.state;
1313
return (
1414
<AutoComplete
1515
{...rest}
1616
ref={(r) => { this.input = r; }}
17-
errorText={!isValid && this.getErrorMessage()}
17+
errorText={(!isValid && this.getErrorMessage()) || errorText}
1818
/>
1919
);
2020
}

src/DateValidator.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ export default class DateValidator extends ValidatorComponent {
88

99
render() {
1010
// eslint-disable-next-line
11-
const { errorMessages, validators, requiredError, ...rest } = this.props;
11+
const { errorMessages, validators, requiredError, errorText, ...rest } = this.props;
1212
const { isValid } = this.state;
1313
return (
1414
<DatePicker
1515
{...rest}
1616
ref={(r) => { this.input = r; }}
17-
errorText={!isValid && this.getErrorMessage()}
17+
errorText={(!isValid && this.getErrorMessage()) || errorText}
1818
/>
1919
);
2020
}

src/SelectValidator.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ export default class SelectValidator extends ValidatorComponent {
88

99
render() {
1010
// eslint-disable-next-line
11-
const { errorMessages, validators, requiredError, ...rest } = this.props;
11+
const { errorMessages, validators, requiredError, errorText, ...rest } = this.props;
1212
const { isValid } = this.state;
1313
return (
1414
<SelectField
1515
{...rest}
1616
ref={(r) => { this.input = r; }}
17-
errorText={!isValid && this.getErrorMessage()}
17+
errorText={(!isValid && this.getErrorMessage()) || errorText}
1818
/>
1919
);
2020
}

src/TextValidator.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ export default class TextValidator extends ValidatorComponent {
88

99
render() {
1010
// eslint-disable-next-line
11-
const { errorMessages, validators, requiredError, ...rest } = this.props;
11+
const { errorMessages, validators, requiredError, errorText, ...rest } = this.props;
1212
const { isValid } = this.state;
1313
return (
1414
<TextField
1515
{...rest}
1616
ref={(r) => { this.input = r; }}
17-
errorText={!isValid && this.getErrorMessage()}
17+
errorText={(!isValid && this.getErrorMessage()) || errorText}
1818
/>
1919
);
2020
}

src/TimeValidator.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ export default class TimeValidator extends ValidatorComponent {
88

99
render() {
1010
// eslint-disable-next-line
11-
const { errorMessages, validators, requiredError, ...rest } = this.props;
11+
const { errorMessages, validators, requiredError, errorText, ...rest } = this.props;
1212
const { isValid } = this.state;
1313
return (
1414
<TimePicker
1515
{...rest}
1616
ref={(r) => { this.input = r; }}
17-
errorText={!isValid && this.getErrorMessage()}
17+
errorText={(!isValid && this.getErrorMessage()) || errorText}
1818
/>
1919
);
2020
}

src/ValidatorComponent.jsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable */
22
import React from 'react';
3+
import PropTypes from 'prop-types';
34
/* eslint-enable */
45
import ValidatorForm from './ValidatorForm';
56

@@ -98,19 +99,19 @@ class ValidatorComponent extends React.Component {
9899
}
99100

100101
ValidatorComponent.contextTypes = {
101-
form: React.PropTypes.object,
102+
form: PropTypes.object,
102103
};
103104

104105
ValidatorComponent.propTypes = {
105-
errorMessages: React.PropTypes.oneOfType([
106-
React.PropTypes.array,
107-
React.PropTypes.string,
106+
errorMessages: PropTypes.oneOfType([
107+
PropTypes.array,
108+
PropTypes.string,
108109
]),
109-
validators: React.PropTypes.array,
110-
name: React.PropTypes.string,
111-
value: React.PropTypes.oneOfType([
112-
React.PropTypes.string,
113-
React.PropTypes.number,
110+
validators: PropTypes.array,
111+
name: PropTypes.string,
112+
value: PropTypes.oneOfType([
113+
PropTypes.string,
114+
PropTypes.number,
114115
]),
115116
};
116117

src/ValidatorForm.jsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable */
22
import React from 'react';
3+
import PropTypes from 'prop-types';
34
/* eslint-enable */
45
import Rules from './ValidationRules';
56

@@ -143,13 +144,13 @@ ValidatorForm.addValidationRule = (name, callback) => {
143144
};
144145

145146
ValidatorForm.childContextTypes = {
146-
form: React.PropTypes.object,
147+
form: PropTypes.object,
147148
};
148149

149150
ValidatorForm.propTypes = {
150-
onSubmit: React.PropTypes.func.isRequired,
151-
instantValidate: React.PropTypes.bool,
152-
children: React.PropTypes.node,
151+
onSubmit: PropTypes.func.isRequired,
152+
instantValidate: PropTypes.bool,
153+
children: PropTypes.node,
153154
};
154155

155156
export default ValidatorForm;

0 commit comments

Comments
 (0)