Skip to content

Commit ac5471f

Browse files
committed
adding support for async validation
1 parent 1b67593 commit ac5471f

File tree

2 files changed

+81
-4
lines changed

2 files changed

+81
-4
lines changed

src/components/FormlyField.vue

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,19 @@
5454
5555
let valid = false;
5656
if ( typeof validator === 'function' ){
57-
valid = !validator(field, model);
57+
//valid = !validator(field, model);
58+
// setup for async validation
59+
validator(field, model, (asyncValid = false, asyncValidatorMessage = validatorMessage) => {
60+
// whenever validation is done via a function we will assume it's asynchronous and will require next() to be called
61+
// this way it doesn't matter if it's async or not, next() should always be called
62+
console.log(this.form);
63+
setError(this.form, this.field.key, validKey, !asyncValid, asyncValidatorMessage);
64+
});
5865
} else {
5966
let res = new Function('model', 'field', 'return '+validator+';' );
6067
valid = !res.call({}, model, field);
68+
setError(this.form, this.field.key, validKey, valid, validatorMessage);
6169
}
62-
setError(this.form, this.field.key, validKey, valid, validatorMessage);
6370
6471
});
6572
}

test/unit/specs/FormlyField.spec.js

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,10 @@ describe('FormlyField', () => {
269269
key: 'search',
270270
type: 'test',
271271
validators: {
272-
expression: function(field, model){
273-
return model.search == 'test';
272+
expression: function(field, model, next){
273+
let valid = model.search == 'test';
274+
next( valid );
275+
//return model.search == 'test';
274276
}
275277
}
276278
}
@@ -286,6 +288,43 @@ describe('FormlyField', () => {
286288
},0);
287289
});
288290

291+
it('Async Validation', (done) => {
292+
let data = {
293+
form: {
294+
$valid: true,
295+
$errors: {}
296+
},
297+
model: {
298+
search: 'testing'
299+
},
300+
fields: [
301+
{
302+
key: 'search',
303+
type: 'test',
304+
validators: {
305+
asyncExpression: function(field, model, next){
306+
console.log('--- first timeout here');
307+
let valid = model.search == 'test';
308+
setTimeout( function(){
309+
console.log('--------------- timeout here');
310+
next( valid );
311+
}, 500);
312+
}
313+
}
314+
}
315+
]
316+
};
317+
318+
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';
322+
setTimeout(()=>{
323+
//expect(vm.form.$errors.search.expression).to.be.false;
324+
//done();
325+
},1000);
326+
});
327+
289328
describe("Validation Messages", (done) => {
290329

291330
//what do we want to do
@@ -331,6 +370,37 @@ describe('FormlyField', () => {
331370
expect(vm.form.$errors.search.validatorMessage).to.equal('Must equal test');
332371
});
333372

373+
it('Inline messages with a function', () => {
374+
let data = {
375+
form: {
376+
$valid: true,
377+
$errors: {}
378+
},
379+
model: {
380+
search: 'testing'
381+
},
382+
fields: [
383+
{
384+
key: 'search',
385+
type: 'test',
386+
validators: {
387+
validatorMessage:
388+
{
389+
expression: function(field, model, next){
390+
let valid = model.search == "test";
391+
next(valid);
392+
},
393+
message: 'Must equal test'
394+
}
395+
}
396+
}
397+
]
398+
};
399+
400+
createValidField(data);
401+
expect(vm.form.$errors.search.validatorMessage).to.equal('Must equal test');
402+
});
403+
334404
it('Inline messages with values parsed', () => {
335405
let data = {
336406
form: {

0 commit comments

Comments
 (0)