According to the release doc, Regula 1.3.0 should support async validation. It seems that compound constraints are able to take an option of async in some way. Does this hold true for custom validations as well? What would be the syntax for this?
Asked
Active
Viewed 133 times
1 Answers
2
I'm still working on finishing the documentation for 1.3 (what I have so far is available here).
Asynchronous support is available for both custom constraints and compound constraints (that contain at least one asynchronous constraint).
The syntax to define an asynchronous constraint is pretty simple:
regula.custom({
name: "MyAsyncContraint",
async: true,
defaultMessage: "The asynchronous constraint failed.",
validator: function(params, validator, callback) {
//Using jQuery as an example
jQuery.ajax({
url: myUrl,
dataType: "jsonp",
success: function(data) {
//Use the callback to pass the result of validation back to
//regula.
callback(data.pass)
}
});
}
});
There is no explicit syntax or call to make a compound constraint asynchronous. Any compound constraint that contains one or more asynchronous is implicitly asynchronous. To validate asynchronous constraints (or even a mix of synchronous and asynchronous constraints), you just pass a callback
to .validate
:
regula.validate(function(constraintViolations) {
...
});
You can use .validate
with options and a callback as well:
regula.validate(options, function(constraintViolations) {
...
});

Vivin Paliath
- 94,126
- 40
- 223
- 295
-
1Appreciate the link to the new docs, even if they're in progress. – sq33G Nov 18 '13 at 19:34
-
@sq33G I have been busy with work which is why I haven't been able to update the documentation. Thank you for being patient! :) – Vivin Paliath Nov 18 '13 at 19:55