10
10
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11
11
*/
12
12
13
+ 'use strict' ;
14
+
13
15
var templates = require ( './templates.jsx' ) ;
14
16
var React = require ( 'react' ) ;
15
17
var marked = require ( 'marked' ) ;
@@ -87,37 +89,29 @@ var CommentForm = React.createClass({
87
89
displayName : 'CommentForm' ,
88
90
getInitialState : function ( ) {
89
91
// 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 (
93
93
{
94
-
94
+ author : 'required' ,
95
+ text : 'required|min:10|max:50'
95
96
} ,
96
97
{
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'
99
100
}
100
101
) ;
101
102
102
103
return { author : '' , text : '' } ;
103
104
} ,
104
105
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 ) ;
112
108
} ,
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 ) ;
121
115
} ) ;
122
116
} ,
123
117
handleSubmit : function ( e ) {
@@ -143,20 +137,54 @@ var CommentForm = React.createClass({
143
137
} ) ;
144
138
145
139
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
+ } ,
146
158
/**
147
159
* Validate using the validatorjs library
148
160
*
149
161
* @see https://www.npmjs.com/package/validatorjs
150
162
*
151
163
* @param {Object } data the data submitted
152
- * @param {Validator } validator the validatorjs validator
164
+ * @param {Object } schema contains rules and custom error messages
153
165
* @param {Object } options contains name of element being validated and previous errors
154
166
* @param {Function } callback called and passed the errors
155
167
*/
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
+ }
160
188
161
189
var getErrors = ( ) => {
162
190
if ( options . key ) {
0 commit comments