User Guide
222 Chapter 6: Components Dictionary
To create and use a custom validator class:
1.
In Flash MX Professional 2004, create a new ActionScript (AS) file.
2.
Add the following code to the AS file:
class OddNumbersOnly extends mx.data.binding.CustomValidator
{
public function validate(value) {
// make sure the value is of type Number
var n = Number(value);
if (String(n) == "NaN") {
this.validationError("'" + value + "' is not a number.");
return;
}
// make sure the number is odd
if (n % 2 == 0) {
this.validationError("'" + value + "' is not an odd number.");
return;
}
// data is OK, no need to do anything, just return
}
}
3.
Save the AS file as OddNumbersOnly.as.
Note: The name of the AS file must match the name of the class.
4.
Create a new Flash (FLA) document.
5.
Open the Components panel.
6.
Drag a NumericStepper component from the Components panel to the Stage and name
it stepper.
7.
Drag a Label component to the Stage and name it textLabel.
8.
Drag a TextArea component to the Stage and name it status.
9.
Select the NumericStepper component, and open the Component inspector.
10.
Select the Bindings tab in the Component inspector, and click the Add Binding (+) button.
11.
Select the Value property (the only one) in the Add Bindings dialog box, and click OK.
12.
In the Component inspector, double-click Bound To in the Binding Attributes pane of the
Bindings tab to open the Bound To dialog box.
13.
In the Bound To dialog box, select the Label component in the Component Path pane and its
text property in the Schema Location pane. Click OK.
14.
Select the Label component on the Stage, and click the Schema tab in the Component
Inspector panel.
15.
In the Schema Attributes pane, select Custom from the Data Type pop-up menu.
16.
Double-click the Validation Options field in the Schema Attributes pane to open the Custom
Validation Settings dialog box.
17.
In the ActionScript Class text box, enter OddNumbersOnly, which is the name of the
ActionScript class you created previously. Click OK.