A port of the form validation classes from Apache Flex (formerly Adobe Flex) to Feathers UI for Haxe and OpenFL.
Includes the following validators:
CreditCardValidator
CurrencyValidator
DateValidator
EmailValidator
NumberValidator
PhoneNumberValidator
RegExpValidator
SocialSecurityValidator
StringValidator
ZipCodeValidator
- Haxe 4.0
- OpenFL 8.9.7
Run the following command in a terminal to install feathersui-validators from Haxelib.
haxelib install feathersui-validators
After installing the library above, add it to your OpenFL project.xml file:
<haxelib name="feathersui-validators" />
The following example validates a text input when it loses focus:
var textInput = new TextInput();
addChild(textInput);
var validator = new NumberValidator();
validator.source = textInput;
validator.valueFunction = () -> textInput.text;
validator.triggerEvent = FocusEvent.FOCUS_OUT;
validator.addEventListener(ValidationResultEvent.VALID, event -> {
textInput.errorString = null;
});
validator.addEventListener(ValidationResultEvent.INVALID, event -> {
var errorString = "";
for (validationResult in event.results) {
if (!validationResult.isError) {
continue;
}
if (errorString.length > 0) {
errorString += "\n";
}
errorString += validationResult.errorMessage;
}
textInput.errorString = errorString;
});
The following example validates a form when it is submitted:
var form = new Form();
addChild(form);
var textInput = new TextInput();
var formItem = new FormItem("My Field", textInput);
form.addChild(formItem);
var submitButton = new Button("Submit");
form.addChild(submitButton);
form.submitButton = submitButton;
var validator = new NumberValidator();
validator.source = null;
validator.valueFunction = () -> textInput.text;
// don't trigger automatically
// we'll do it manually when the form is submitted
validator.triggerEvent = null;
validator.addEventListener(ValidationResultEvent.VALID, event -> {
textInput.errorString = null;
});
validator.addEventListener(ValidationResultEvent.INVALID, event -> {
var errorString = "";
for (validationResult in event.results) {
if (!validationResult.isError) {
continue;
}
if (errorString.length > 0) {
errorString += "\n";
}
errorString += validationResult.errorMessage;
}
textInput.errorString = errorString;
});
form.addEventListener(FormEvent.SUBMIT, event -> {
var hasErrors = false;
var validators:Array<IValidator> = [validator];
var events = Validator.validateAll(validators);
for (event in events) {
for (validationResult in event.results) {
if (validationResult.isError) {
hasErrors = true;
break;
}
}
if (hasErrors) {
break;
}
}
if (hasErrors) {
// some checks were invalid, so don't submit
return;
}
// everything is valid, so now it can be sent to the server
// using URLLoader or something
});