-
Notifications
You must be signed in to change notification settings - Fork 3
Appendix A: Validators
This tutorial will not build any more functionality into your extension. In the previous tutorial we used a ContextValidator
to ensure that a particular type of Context
was available when our CommandHandler
was executed. But how do validators in general work?
Firstly, all validators implement the core.app.core.validators.IValidator
interface.
Secondly, most validators extends extends the core.app.core.validators.AbstractValidator
class.
package core.app.validators
{
import flash.events.EventDispatcher;
import core.app.core.validators.IValidator;
import core.app.events.ValidatorEvent;
[Event(name="stateChanged", type="core.app.events.ValidatorEvent")]
public class AbstractValidator extends EventDispatcher implements IValidator
{
protected var _state :Boolean = false;
private var firstSet :Boolean = false;
public function AbstractValidator()
{
}
public function dispose():void {}
public function get state():Boolean { return _state; }
protected function setState( value:Boolean ):void
{
if ( value == _state && !firstSet ) return;
firstSet = true;
_state = value;
dispatchEvent( new ValidatorEvent( ValidatorEvent.STATE_CHANGED, _state ) );
}
}
}
AbstractValidator
has a read-only 'state' property, and dispatches an 'stageChanged' event when this changes. Broadly speaking, a Validator wraps up a Boolean value. It usually takes one or more managers as input (usually in its constructor) and some sort of matching criteria. In the case of ContextValidator
, it takes an instance of a ContextManager
to monitor, and a type of Context to match to. Internally it will set its 'state' parameter to true when the ContextManager
owns a Context matching the type. And false if not.
The CoreApp framework contains a number of generic Validators such as the CollectionValidator
. This is a particularly useful validator.
var collectionValidator:CollectionValidator = new CollectionValidator( myCollection );
collectionValidator.validType = String;
collectionValidator.min = 1;
collectionValidator.max = 10;
In this example we are telling the CollectionValidator
to monitor myCollection
(an ArrayCollection
). The validator's state will be true only if it contains between 1 and 10 items (inclusive) that are of the type validType
.
There is also a ContextSelectionValidator
which combines the functionality of a ContextValidator
and a CollectionValidator
to validate if a particular type of Context has a certain selection. This is a validator you will use frequently when building extensions.
Also in core.app.core.validators
is the CompoundValidator
class. This allows you to combine multiple Validators together using logical operators like AND
, OR
and XOR
.
var compoundValidator:CompoundValidator = new CompoundValidator();
compoundValidator.addValidator( new CollectionValidator( myCollectionA, String, 1, 5 ) );
compoundValidator.addValidator( new CollectionValidator( myCollectionB, Number, 1, 1 ) );
compoundValidator.operation = CompoundValidator.XOR;
In this example our CompoundValidator's state will be true only if myCollectionA
has between 1-5 Strings
or myCollectionB
has 1 Number
, but will be false if they are both true or both false (as we are using XOR
, exclusive OR - one or the other, but not both).