ShowTable of Contents
A class to make field validation simple.
This class offers methods to add validation to fields and to run through all validations at once. When one validation fails, the field is visually marked and the user gets a notification.
Usage
This example shows how to create a new YNValidatorRunner instance and add a simple "check if field is empty" test to two fields.
You can use the code for example in the click event of a "save" button.
var validatorRunner = new YNValidatorRunner();
var validator = new YNValidator(App.inputUsername, "Username");
validator.enableTestEmpty();
validatorRunner.add(validator);
var validator = new YNValidator(App.inputPassword, "Password");
validator.enableTestEmpty();
validatorRunner.add(validator);
if(!validatorRunner.run(true, true)) return false;
YNValidatorRunner
Create:
var validatorRunner = new YNValidatorRunner();
Run through all validations:
if (validatorRunner.run(markField, displayMsg, setFocus)) {
// do something when the validation succeeded
} else {
// do something when at least one validator failed
}
with:
markField: set to true to mark a field that didn't pass it's validitation
displayMsg: set to true to put out an alert to the user if one validation failed
setFocus: set to true to put focus to the first field that didn't pass it's validation
YNValidator
Create one validator per field. Each validator may have one or multiple tests enabled.
Create:
var validator = new YNValidator(field, fieldlabel)
with:
field: the Ti.UI field, for example a Ti.UI.TextField
fieldlabel: a label for the field (as string)
Tests
The following tests are currently implemented in a validator:
YNValidator.enableTestEmpty(): checks if the field is empty.
Add a validator to the validatorRunner
You have to add each validator for each field to the validatorRunner:
validatorRunner.add(validator)