To create and use a custom validator class:
In Flash MX Professional 2004, create a new ActionScript (AS) file.
1.
Add the following code to the AS file:
2.
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
}
}
Save the AS file as OddNumbersOnly.as.
3.
Note: The name of the AS file must match the name of the class.
Create a new Flash (FLA) document.
4.
Open the Components panel.
5.
Drag a NumericStepper component from the Components panel to the Stage and name
6.
it stepper.
Drag a Label component to the Stage and name it textLabel.
7.
Drag a TextArea component to the Stage and name it status.
8.
Select the NumericStepper component, and open the Component inspector.
9.
Select the Bindings tab in the Component inspector, and click the Add Binding (+) button.
10.
Select the Value property (the only one) in the Add Bindings dialog box, and click OK.
11.
In the Component inspector, double-click Bound To in the Binding Attributes pane of the
12.
Bindings tab to open the Bound To dialog box.
In the Bound To dialog box, select the Label component in the Component Path pane and its
13.
property in the Schema Location pane. Click OK.
text
Select the Label component on the Stage, and click the Schema tab in the Component
14.
Inspector panel.
In the Schema Attributes pane, select Custom from the Data Type pop-up menu.
15.
Double-click the Validation Options field in the Schema Attributes pane to open the Custom
16.
Validation Settings dialog box.
In the ActionScript Class text box, enter OddNumbersOnly, which is the name of the
17.
ActionScript class you created previously. Click OK.
222
Chapter 6: Components Dictionary
Need help?
Do you have a question about the FLASH MX 2004-USING COMPONENTS and is the answer not in the manual?