Skip to content

Commit 4196a24

Browse files
committed
further additions to async validation
1 parent ac5471f commit 4196a24

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
"babel-loader": "6.2.4",
3030
"babel-plugin-transform-runtime": "6.12.0",
3131
"babel-preset-es2015": "6.9.0",
32-
"babel-preset-stage-0": "6.5.0",
3332
"babel-preset-es2015-loose-rollup": "^7.0.0",
33+
"babel-preset-stage-0": "6.5.0",
3434
"chai": "3.5.0",
3535
"conventional-changelog-cli": "1.2.0",
3636
"conventional-github-releaser": "1.1.3",
@@ -74,6 +74,7 @@
7474
"sinon-chai": "2.8.0",
7575
"style-loader": "0.13.1",
7676
"uglify-js": "^2.6.4",
77+
"vue": "^2.2.6",
7778
"vue-hot-reload-api": "1.3.2",
7879
"vue-html-loader": "1.2.3",
7980
"vue-loader": "8.5.3",
@@ -82,5 +83,9 @@
8283
"webpack-dev-middleware": "1.6.1",
8384
"webpack-dev-server": "1.14.1",
8485
"webpack-merge": "0.14.1"
86+
},
87+
"dependencies": {
88+
"babel-runtime": "^6.23.0",
89+
"vue": "^2.2.6"
8590
}
8691
}

src/components/FormlyField.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,16 @@
5454
5555
let valid = false;
5656
if ( typeof validator === 'function' ){
57-
//valid = !validator(field, model);
57+
//set the asynchronous flag so that we know it's going
58+
let asyncKey = '$async_'+validKey;
59+
this.$set(this.form.$errors[ this.field.key ], asyncKey, true);
60+
5861
// setup for async validation
5962
validator(field, model, (asyncValid = false, asyncValidatorMessage = validatorMessage) => {
6063
// whenever validation is done via a function we will assume it's asynchronous and will require next() to be called
6164
// this way it doesn't matter if it's async or not, next() should always be called
62-
console.log(this.form);
6365
setError(this.form, this.field.key, validKey, !asyncValid, asyncValidatorMessage);
66+
this.$set(this.form.$errors[ this.field.key ], asyncKey, false);
6467
});
6568
} else {
6669
let res = new Function('model', 'field', 'return '+validator+';' );

test/unit/karma.conf.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ var wsConfig = require('./webpack.test.config');
33
module.exports = function(config) {
44
var settings = {
55
// base path that will be used to resolve all patterns (eg. files, exclude)
6-
basePath: '',
6+
basePath: '',
7+
plugins: ['karma-mocha', 'karma-chai', 'karma-sinon-chai', 'karma-webpack', 'karma-sourcemap-loader', 'karma-spec-reporter', 'karma-coverage', 'karma-phantomjs-launcher'],
78

89
browsers: ['PhantomJS'],
910

@@ -45,4 +46,4 @@ module.exports = function(config) {
4546
}
4647

4748
config.set(settings);
48-
}
49+
}

test/unit/specs/FormlyField.spec.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,8 @@ describe('FormlyField', () => {
303303
type: 'test',
304304
validators: {
305305
asyncExpression: function(field, model, next){
306-
console.log('--- first timeout here');
307306
let valid = model.search == 'test';
308307
setTimeout( function(){
309-
console.log('--------------- timeout here');
310308
next( valid );
311309
}, 500);
312310
}
@@ -316,12 +314,14 @@ describe('FormlyField', () => {
316314
};
317315

318316
createValidField(data);
319-
console.log('expression is ', vm.form.$errors.search.asyncExpression);
320-
expect(vm.form.$errors.search.asyncExpression).to.be.true;
321-
vm.model.search = 'test';
317+
expect(vm.form.$errors.search.asyncExpression).to.be.false;
318+
expect(vm.form.$errors.search.$async_asyncExpression).to.be.true;
319+
vm.model.search = 'test';
322320
setTimeout(()=>{
323-
//expect(vm.form.$errors.search.expression).to.be.false;
324-
//done();
321+
expect(vm.form.$errors.search.asyncExpression).to.be.false;
322+
expect(vm.form.$errors.search.$async_asyncExpression).to.be.false;
323+
expect(vm.form.$valid).to.be.true;
324+
done();
325325
},1000);
326326
});
327327

0 commit comments

Comments
 (0)