1

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?

sq33G
  • 3,320
  • 1
  • 23
  • 38

1 Answers1

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