Skip to content

Commit c3bfdc1

Browse files
author
Dan Moore
committed
In the middle of validation work
1 parent b342493 commit c3bfdc1

File tree

2 files changed

+58
-28
lines changed

2 files changed

+58
-28
lines changed

components/components.js

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1111
*/
1212

13+
'use strict';
14+
1315
var templates = require('./templates.jsx');
1416
var React = require('react');
1517
var marked = require('marked');
@@ -87,37 +89,29 @@ var CommentForm = React.createClass({
8789
displayName: 'CommentForm',
8890
getInitialState: function () {
8991
// Define the rules and custom messages for each field.
90-
// Do this by creating a validator with no data; it's added later.
91-
this.validatorTypes = new Validator(
92-
{},
92+
this.validatorTypes = strategy.createInactiveSchema(
9393
{
94-
94+
author: 'required',
95+
text: 'required|min:10|max:50'
9596
},
9697
{
97-
'min.text': 'Enter a message between 10 and 50 characters',
98-
'max.text': 'Enter a message between 10 and 50 characters'
98+
//'min.text': 'Enter a message between 10 and 50 characters',
99+
//'max.text': 'Enter a message between 10 and 50 characters'
99100
}
100101
);
101102

102103
return {author: '', text: ''};
103104
},
104105
addValidation: function(e) {
105-
this.validatorTypes.rules = this.validatorTypes._parseRules({
106-
author: 'required',
107-
text: 'required|min:10|max:50'
108-
});
109-
110-
this.props.handleValidation('author')(e);
111-
this.props.handleValidation('text')(e);
106+
strategy.activateRule(this.validatorTypes, e.target.name);
107+
this.props.handleValidation(e.target.name)(e);
112108
},
113-
handleAuthorChange: function (e) {
114-
this.setState({author: e.target.value}, () => {
115-
this.props.handleValidation('author')(e);
116-
});
117-
},
118-
handleTextChange: function (e) {
119-
this.setState({text: e.target.value}, () => {
120-
this.props.handleValidation('text')(e);
109+
handleChange: function (e) {
110+
var state = {};
111+
state[e.target.name] = e.target.value;
112+
113+
this.setState(state, () => {
114+
this.props.handleValidation(e.target.name)(e);
121115
});
122116
},
123117
handleSubmit: function (e) {
@@ -143,20 +137,54 @@ var CommentForm = React.createClass({
143137
});
144138

145139
var strategy = {
140+
createSchema: function (rules, messages, createValidatorCallback) {
141+
return {
142+
rules,
143+
messages,
144+
createValidatorCallback
145+
};
146+
},
147+
createInactiveSchema: function (rules, messages, createValidatorCallback) {
148+
var schema = this.createSchema(rules, messages, createValidatorCallback);
149+
schema.activeRules = [];
150+
151+
return schema;
152+
},
153+
activateRule: function(schema, rule) {
154+
if (schema.activeRules.indexOf(rule) === -1) {
155+
schema.activeRules.push(rule);
156+
}
157+
},
146158
/**
147159
* Validate using the validatorjs library
148160
*
149161
* @see https://www.npmjs.com/package/validatorjs
150162
*
151163
* @param {Object} data the data submitted
152-
* @param {Validator} validator the validatorjs validator
164+
* @param {Object} schema contains rules and custom error messages
153165
* @param {Object} options contains name of element being validated and previous errors
154166
* @param {Function} callback called and passed the errors
155167
*/
156-
validate: function(data, validator, options, callback) {
157-
// Set the data again on the validator and clear existing errors
158-
validator.input = data;
159-
validator.errors.errors = {};
168+
validate: function (data, schema, options, callback) {
169+
var rules = {};
170+
171+
// Only add active rules to the validator if an initially inactive schema has been created.
172+
// Check all rules regardless if the form has been submitted (options.key is empty).
173+
if (typeof schema.activeRules !== 'undefined' && options.key) {
174+
for (let i in schema.activeRules) {
175+
let ruleName = schema.activeRules[i];
176+
177+
rules[ruleName] = schema.rules[ruleName];
178+
}
179+
} else {
180+
rules = schema.rules;
181+
}
182+
183+
var validator = new Validator(data, rules, schema.messages);
184+
185+
if (typeof schema.createValidatorCallback === 'function') {
186+
schema.createValidatorCallback(validator);
187+
}
160188

161189
var getErrors = () => {
162190
if (options.key) {

components/templates.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ module.exports = {
5050
<input
5151
type="text"
5252
placeholder="Your name"
53+
name="author"
5354
className={this.getClasses('author')}
5455
value={this.state.author}
55-
onChange={this.handleAuthorChange}
56+
onChange={this.handleChange}
5657
onBlur={this.addValidation}
5758
/>
5859
</p>
@@ -63,9 +64,10 @@ module.exports = {
6364
<input
6465
type="text"
6566
placeholder="Say something..."
67+
name="text"
6668
className={this.getClasses('text')}
6769
value={this.state.text}
68-
onChange={this.handleTextChange}
70+
onChange={this.handleChange}
6971
onBlur={this.addValidation}
7072

7173
/>

0 commit comments

Comments
 (0)