Skip to content

Commit 5a1ec40

Browse files
committed
added asyncFuncCall - Validates a field using an asynchronous third party function call
1 parent ab894c7 commit 5a1ec40

File tree

4 files changed

+352
-79
lines changed

4 files changed

+352
-79
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ The following attribute's value will be loaded for the relative validation rule:
132132
* custom
133133
* ajax
134134
* funcCall
135+
* asyncFuncCall
135136

136137
##### data-errormessage
137138
* a generic fall-back error message
@@ -500,6 +501,30 @@ The following declaration will do
500501
<input value="" class="validate[required,funcCall[checkHELLO]]" type="text" id="lastname" name="lastname" />
501502
```
502503

504+
### asyncFuncCall[methodName]
505+
506+
Validates a field using an asynchronous third party function call. This behaves similarly to funcCall, but the external function is an asynchronous function - it does not return anything. Instead it must call a callback with the validation result (true if success, false if failure) and an optional message to display. This allows you to validate anything asynchronously (i.e. call ajax, call some 3rd party library, use websockets, etc.)
507+
508+
```js
509+
function checkHELLO(field, rules, i, options, callback){
510+
setTimeout(function(){
511+
if (field.val() === "HELLO") {
512+
callback(true);
513+
}
514+
else {
515+
var msg = options.allrules.validate2fields.alertText; // this allows to use i18 for the error msgs
516+
callback(false, msg);
517+
}
518+
},2000);
519+
}
520+
```
521+
522+
The following declaration will do
523+
524+
```html
525+
<input value="" class="validate[required,asyncFuncCall[checkHELLO]]" type="text" id="lastname" name="lastname" />
526+
```
527+
503528
### ajax[selector]
504529

505530
Delegates the validation to a server URL using an asynchronous Ajax request. The selector is used to identify a block of properties in the translation file, take the following for example.

demos/demoValidators.html

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@
3131
return options.allrules.validate2fields.alertText;
3232
}
3333
}
34+
35+
/**
36+
*
37+
* @param {jqObject} the field where the validation applies
38+
* @param {Array[String]} validation rules for this field
39+
* @param {int} rule index
40+
* @param {Map} form options
41+
* @returns nothing - call the callback with the result
42+
* function must call the callback with the following parameters:
43+
* @param {boolean} status - true (success) or false (failure)
44+
* @param {string} msg - optional message to display
45+
*/
46+
function asyncCheckHELLO(field, rules, i, options, callback){
47+
setTimeout(function(){
48+
if (field.val() === "HELLO") {
49+
callback(true);
50+
}
51+
else {
52+
var msg = options.allrules.validate2fields.alertText; // this allows to use i18 for the error msgs
53+
callback(false, msg);
54+
}
55+
},2000);
56+
}
3457
</script>
3558
</head>
3659
<body>
@@ -136,7 +159,19 @@
136159
validate[required,funcCall[checkHELLO]]
137160
</label>
138161
</fieldset>
139-
162+
163+
<fieldset>
164+
<legend>
165+
Asynchronous Function
166+
</legend>
167+
<label>
168+
<span>Write 'HELLO' : </span>
169+
<input value="" class="validate[required,asyncFuncCall[asyncCheckHELLO]] text-input" type="text" id="lastname2" name="lastname2" />
170+
<br/>
171+
validate[required,asyncFuncCall[asyncCheckHELLO]]
172+
</label>
173+
</fieldset>
174+
140175
<fieldset>
141176
<legend>
142177
Conditional required

0 commit comments

Comments
 (0)