Flash CS3 Documentation |
|||
| ActionScript 2.0 Components Language Reference > Data binding classes > CustomValidator.validate() | |||
Flash Player 6 (6.0.79.0).
Flash MX Professional 2004.
This method is called automatically; you don't invoke it directly.
value The data to be validated; it can be of any type.
Nothing.
Method; called automatically to validate the data contained by the value parameter. You must implement this method in your subclass of CustomValidator; the default implementation does nothing.
You can use any ActionScript code to examine and validate the data. If the data is not valid, this method should call this.validationError() with an appropriate message. You can call this.validationError() more than once if there are several validation problems with the data.
Since validate() might be called repeatedly, avoid adding code that takes a long time to complete. Your implementation of this method should only check for validity, and then report any errors using CustomValidator.validationError(). Similarly, your implementation should not take any action as a result of the validation test, such as alerting the end user. Instead, create event listeners for valid and invalid events and alert the end user from those event listeners (see the example below).
The following procedure demonstrates how to create and use a custom validator class. The validate() method of the CustomValidator class OddNumbersOnly.as determines any value that is not an odd number to be invalid. The validation occurs whenever a change occurs in the value of a NumericStepper component, which is bound to the text property of a Label component.
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
}
}
|
NOTE |
|
The name of the AS file must match the name of the class. |
text property in the Schema Location pane. Click OK.
function dataIsInvalid(evt) {
if (evt.property == "text") {
status.text = evt.messages;
}
}
function dataIsValid(evt) {
if (evt.property == "text") {
status.text = "OK";
}
}
textLabel.addEventListener("valid", dataIsValid);
textLabel.addEventListener("invalid", dataIsInvalid);
Click the arrows on the NumericStepper component to change its value. Notice the message that appears in the TextArea component when you choose even and odd numbers.
Flash CS3
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00002676.html