feathersui / feathersui-validators

A port of the form validation classes from Apache Flex (formerly Adobe Flex) to Feathers UI for Haxe and OpenFL

Home Page:https://api.feathersui.com/validators/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Validators for Feathers UI

A port of the form validation classes from Apache Flex (formerly Adobe Flex) to Feathers UI for Haxe and OpenFL.

Includes the following validators:

Minimum Requirements

  • Haxe 4.0
  • OpenFL 8.9.7

Installation

Run the following command in a terminal to install feathersui-validators from Haxelib.

haxelib install feathersui-validators

Project Configuration

After installing the library above, add it to your OpenFL project.xml file:

<haxelib name="feathersui-validators" />

Usage

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
});

Documentation

About

A port of the form validation classes from Apache Flex (formerly Adobe Flex) to Feathers UI for Haxe and OpenFL

https://api.feathersui.com/validators/

License:Apache License 2.0


Languages

Language:Haxe 99.4%Language:JavaScript 0.3%Language:HTML 0.3%