|
15 | 15 | methods: {
|
16 | 16 | validate:function(){
|
17 | 17 |
|
18 |
| - //first check if we need to create a field |
19 |
| - if ( !this.form.$errors[this.field.key] ) this.$set(this.form.$errors, this.field.key, {}); |
20 |
| - if ( !this.field.templateOptions ) this.$set(this.field, 'templateOptions', {}); |
21 |
| - |
22 |
| - //check for required fields. This whole setting,unsetting thing seems kind of wrong though.. |
23 |
| - //there might be a more 'vue-ey' way to do this... |
24 |
| - if ( this.field.required ){ |
25 |
| - if ( !this.form.$errors[this.field.key].required ) this.$set(this.form.$errors[ this.field.key ], 'required', true); |
26 |
| - setError(this.form, this.field.key, 'required', !this.model[ this.field.key ]) ; |
27 |
| - } |
28 |
| - |
29 |
| - //if we've got nothing left then return |
30 |
| - if ( !this.field.validators ) return; |
31 |
| -
|
32 |
| - //set these for the validators so we don't have to use 'this' in them |
33 |
| - let model = this.model; |
34 |
| - let field = this.field; |
35 |
| - |
36 |
| - Object.keys(this.field.validators).forEach((validKey) => { |
37 |
| - if ( !this.form.$errors[this.field.key][validKey] ) this.$set(this.form.$errors[ this.field.key ], validKey, false); |
38 |
| - if ( !this.field.required && !this.model[ this.field.key ] ) { |
39 |
| - setError(this.form, this.field.key, validKey, false); |
40 |
| - return; |
| 18 | + return new Promise((resolve, reject)=>{ |
| 19 | + //first check if we need to create a field |
| 20 | + if ( !this.form.$errors[this.field.key] ) this.$set(this.form.$errors, this.field.key, {}); |
| 21 | + if ( !this.field.templateOptions ) this.$set(this.field, 'templateOptions', {}); |
| 22 | + |
| 23 | + //check for required fields. This whole setting,unsetting thing seems kind of wrong though.. |
| 24 | + //there might be a more 'vue-ey' way to do this... |
| 25 | + if ( this.field.required ){ |
| 26 | + if ( !this.form.$errors[this.field.key].required ) this.$set(this.form.$errors[ this.field.key ], 'required', true); |
| 27 | + setError(this.form, this.field.key, 'required', !this.model[ this.field.key ]) ; |
41 | 28 | }
|
| 29 | + |
| 30 | + //if we've got nothing left then return |
| 31 | + if ( !this.field.validators ) return resolve(); |
42 | 32 |
|
43 |
| - let validator = this.field.validators[validKey]; |
44 |
| - let validatorMessage = false; |
| 33 | + //set these for the validators so we don't have to use 'this' in them |
| 34 | + let model = this.model; |
| 35 | + let field = this.field; |
| 36 | + |
| 37 | + Object.keys(this.field.validators).forEach((validKey) => { |
| 38 | + if ( !this.form.$errors[this.field.key][validKey] ) this.$set(this.form.$errors[ this.field.key ], validKey, false); |
| 39 | + if ( !this.field.required && !this.model[ this.field.key ] ) { |
| 40 | + setError(this.form, this.field.key, validKey, false); |
| 41 | + return resolve(); |
| 42 | + } |
45 | 43 |
|
46 |
| - if ( typeof validator === 'object' ){ |
47 |
| - if ( !( 'message' in validator ) ){ |
48 |
| - console.error( "Looks like you've set a validator object without setting a message. If you don't need to explicity set the message just define the validator as either an expression or a function. Refer to the docs for more info"); |
49 |
| - } else { |
50 |
| - validatorMessage = validator.message; |
51 |
| - validator = validator.expression; |
| 44 | + let validator = this.field.validators[validKey]; |
| 45 | + let validatorMessage = false; |
| 46 | +
|
| 47 | + if ( typeof validator === 'object' ){ |
| 48 | + if ( !( 'message' in validator ) ){ |
| 49 | + console.error( "Looks like you've set a validator object without setting a message. If you don't need to explicity set the message just define the validator as either an expression or a function. Refer to the docs for more info"); |
| 50 | + } else { |
| 51 | + validatorMessage = validator.message; |
| 52 | + validator = validator.expression; |
| 53 | + } |
52 | 54 | }
|
53 |
| - } |
54 |
| - |
55 |
| - let label = ( 'templateOptions' in this.field ) && ( 'label' in this.field.templateOptions ) ? this.field.templateOptions.label : ''; |
56 |
| - validatorMessage = parseValidationString( validKey, validatorMessage, label, model[ this.field.key ] ); |
| 55 | + |
| 56 | + let label = ( 'templateOptions' in this.field ) && ( 'label' in this.field.templateOptions ) ? this.field.templateOptions.label : ''; |
| 57 | + validatorMessage = parseValidationString( validKey, validatorMessage, label, model[ this.field.key ] ); |
57 | 58 |
|
58 |
| - let valid = false; |
59 |
| - if ( typeof validator === 'function' ){ |
60 |
| - //set the asynchronous flag so that we know it's going |
61 |
| - let asyncKey = '$async_'+validKey; |
62 |
| - this.$set(this.form.$errors[ this.field.key ], asyncKey, true); |
63 |
| - |
64 |
| - // setup for async validation |
65 |
| - validator(field, model, (asyncValid = false, asyncValidatorMessage = validatorMessage) => { |
66 |
| - // whenever validation is done via a function we will assume it's asynchronous and will require next() to be called |
67 |
| - // this way it doesn't matter if it's async or not, next() should always be called |
68 |
| - setError(this.form, this.field.key, validKey, !asyncValid, asyncValidatorMessage); |
69 |
| - this.$set(this.form.$errors[ this.field.key ], asyncKey, false); |
70 |
| - }); |
71 |
| - } else { |
72 |
| - let res = new Function('model', 'field', 'return '+validator+';' ); |
73 |
| - valid = !res.call({}, model, field); |
74 |
| - setError(this.form, this.field.key, validKey, valid, validatorMessage); |
75 |
| - } |
76 |
| - |
| 59 | + let valid = false; |
| 60 | + if ( typeof validator === 'function' ){ |
| 61 | + //set the asynchronous flag so that we know it's going |
| 62 | + let asyncKey = '$async_'+validKey; |
| 63 | + this.$set(this.form.$errors[ this.field.key ], asyncKey, true); |
| 64 | + |
| 65 | + // setup for async validation |
| 66 | + validator(field, model, (asyncValid = false, asyncValidatorMessage = validatorMessage) => { |
| 67 | + // whenever validation is done via a function we will assume it's asynchronous and will require next() to be called |
| 68 | + // this way it doesn't matter if it's async or not, next() should always be called |
| 69 | + setError(this.form, this.field.key, validKey, !asyncValid, asyncValidatorMessage); |
| 70 | + this.$set(this.form.$errors[ this.field.key ], asyncKey, false); |
| 71 | + resolve(); |
| 72 | + }); |
| 73 | + } else { |
| 74 | + let res = new Function('model', 'field', 'return '+validator+';' ); |
| 75 | + valid = !res.call({}, model, field); |
| 76 | + setError(this.form, this.field.key, validKey, valid, validatorMessage); |
| 77 | + resolve(); |
| 78 | + } |
| 79 | + |
| 80 | + }); |
77 | 81 | });
|
78 | 82 | }
|
79 | 83 | },
|
|
0 commit comments