Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added asyncFuncCall - Validates a field using an asynchronous third party function #783

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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