Macromedia flex 2 Manual
222
Creating Custom Validators
Example: Validating multiple fields
A validator can validate more than one field at a time. For example, you could create a custom
validator called NameValidator to validate three input controls that represent a person’s first,
middle, and last names.
validator called NameValidator to validate three input controls that represent a person’s first,
middle, and last names.
To create a validator that examines multiple fields, you can either define properties on the
validator that let you specify the multiple input fields, as does the Flex
validator that let you specify the multiple input fields, as does the Flex
DateValidator
class
class, or you can require that the single item passed to the validator includes all of the fields to
be validated.
be validated.
In the following example, you use a NameValidator that validates an item that contains three
fields named
fields named
first
,
middle
, and
last
:
<?xml version="1.0" ?>
<!-- validators/MainNameValidator.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:MyComp="myValidators.*">
<!-- validators/MainNameValidator.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:MyComp="myValidators.*">
<mx:Model id="person">
<name>
<custName>
<first>{firstInput.text}</first>
<middle>{middleInput.text}</middle>
<last>{lastInput.text}</last>
</custName>
</name>
</mx:Model>
<name>
<custName>
<first>{firstInput.text}</first>
<middle>{middleInput.text}</middle>
<last>{lastInput.text}</last>
</custName>
</name>
</mx:Model>
<mx:TextInput id="firstInput"/>
<mx:TextInput id="middleInput"/>
<mx:TextInput id="lastInput"/>
<mx:TextInput id="middleInput"/>
<mx:TextInput id="lastInput"/>
<MyComp:NameValidator id="nameVal"
source="{person}" property="custName"
listener="{firstInput}"/>
<mx:Button label="Validate" click="nameVal.validate();"/>
source="{person}" property="custName"
listener="{firstInput}"/>
<mx:Button label="Validate" click="nameVal.validate();"/>
</mx:Application>
This validator examines three input fields. You specify firstInput as the validation listener.
Therefore, when a validation error occurs, Flex shows a validation error message on the first
Therefore, when a validation error occurs, Flex shows a validation error message on the first
TextInput
control.