-
Notifications
You must be signed in to change notification settings - Fork 24
regula.validate
regula.validate
performs the actual validation. When regula.bind runs, it builds a list of elements and all constraints bound to an element. With regula.validate
it is possible to validate all elements and constraints, a specific element, or a specific element with a specific constraint (assuming that constraint has been bound to the element).
Syntax for regula.validate
is:
regula.validate([elementId[, constraintType]]);
validate
has two optional parameters:
Name | Type | Required | Description |
---|---|---|---|
elementId |
String | no | The id of the element that you want to validate. |
constraintType |
ConstraintType | no | The constraint against which you want to validate the specified element. The constraint should have been bound to the element in markup. |
validate
returns an array of ValidationResult
object-literals. Each ValidationResult
object has the following properties:
Name | Type | Description |
---|---|---|
constraintName |
String | The name of the constraint that failed. |
custom |
boolean | A flag that denotes whether the constraint that failed was a custom constraint or not. |
constraintParameters |
Array | The parameters that this constraint accepts. |
receivedParameters |
Map | A map where the key is the parameter name. The value of each key is the value assigned to the parameter name in markup. |
failingElements |
Array | Array of elements that failed the constraint. This array will only contain one element if the constraint that failed was not a form-specific constraint. Otherwise, the array can contain more than one element. |
message |
String | The error message from the constraint. This is what you probably want to display to the user. |
validate
will throw an exception if you either try to validate an element that has no constraints attached to it, or if you try to validate an element with a constraint that has not been bound to the element.
var validationResults = regula.validate(); var messages = "";
for(var index in validationResults) { var validationResult = validationResults[index]; messages += validationResult.message + "\n"; }
if(messages != "") { alert(messages); }
var validationResults = regula.validate("someElementId"); var messages = "";
for(var index in validationResults) { var validationResult = validationResults[index]; messages += validationResult.message + "\n"; }
if(messages != "") { alert(messages); }
var validationResults = regula.validate("someElementId", regula.ConstraintType.NotEmpty); var message = validationResults[0].message;
if(message != "") { alert(messages); }
Note: It is also possible to validate against a custom constraint. Whenever you add a custom constraint, regula.ConstraintType is updated to contain your custom constraint. Therefore, if you had a custom constraint called MyCustomConstraint
, you can do regula.ConstraintType.MyCustomConstraint
.