Skip to content

Commit

Permalink
added asyncFuncCall - Validates a field using an asynchronous third p…
Browse files Browse the repository at this point in the history
…arty function call
  • Loading branch information
jkapelner committed Apr 11, 2015
1 parent 22e5929 commit 6df21b1
Show file tree
Hide file tree
Showing 4 changed files with 352 additions and 79 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ The following attribute's value will be loaded for the relative validation rule:
* custom
* ajax
* funcCall
* asyncFuncCall

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

### asyncFuncCall[methodName]

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.)

```js
function checkHELLO(field, rules, i, options, callback){
setTimeout(function(){
if (field.val() === "HELLO") {
callback(true);
}
else {
var msg = options.allrules.validate2fields.alertText; // this allows to use i18 for the error msgs
callback(false, msg);
}
},2000);
}
```

The following declaration will do

```html
<input value="" class="validate[required,asyncFuncCall[checkHELLO]]" type="text" id="lastname" name="lastname" />
```

### ajax[selector]

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.
Expand Down
37 changes: 36 additions & 1 deletion demos/demoValidators.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,29 @@
return options.allrules.validate2fields.alertText;
}
}

/**
*
* @param {jqObject} the field where the validation applies
* @param {Array[String]} validation rules for this field
* @param {int} rule index
* @param {Map} form options
* @returns nothing - call the callback with the result
* function must call the callback with the following parameters:
* @param {boolean} status - true (success) or false (failure)
* @param {string} msg - optional message to display
*/
function asyncCheckHELLO(field, rules, i, options, callback){
setTimeout(function(){
if (field.val() === "HELLO") {
callback(true);
}
else {
var msg = options.allrules.validate2fields.alertText; // this allows to use i18 for the error msgs
callback(false, msg);
}
},2000);
}
</script>
</head>
<body>
Expand Down Expand Up @@ -136,7 +159,19 @@
validate[required,funcCall[checkHELLO]]
</label>
</fieldset>


<fieldset>
<legend>
Asynchronous Function
</legend>
<label>
<span>Write 'HELLO' : </span>
<input value="" class="validate[required,asyncFuncCall[asyncCheckHELLO]] text-input" type="text" id="lastname2" name="lastname2" />
<br/>
validate[required,asyncFuncCall[asyncCheckHELLO]]
</label>
</fieldset>

<fieldset>
<legend>
Conditional required
Expand Down
Loading

0 comments on commit 6df21b1

Please sign in to comment.