Macromedia flex 2 Manual

Página de 254
212
Creating Custom Formatters
Handling errors with the SwitchSymbolFormatter 
class
Unlike other formatters, the 
SwitchSymbolFormatter
 class does not write its error messages 
into an 
error
 property. Instead, it is your responsibility to test for error conditions and return 
an error message if appropriate.
The custom formatter component in the following example formats nine-digit Social Security 
numbers by using the SwitchSymbolFormatter class:
package myFormatters
{
    // formatters/myFormatter/CustomSSFormatter.as
    import mx.formatters.Formatter
    import mx.formatters.SwitchSymbolFormatter
    public class CustomSSFormatter extends Formatter 
    {
        // Declare the variable to hold the pattern string.
        public var formatString : String = "###-##-####";
        // Constructor
        public function CustomSSFormatter() {
            // Call base class constructor.
            super();
        }
        // Override format().
        override public function format( value:Object ):String {
            // Validate input string value - must be a 9-digit number.
            // You must explicitly check if the value is a number.
            // The formatter does not do that for you. 
            if( !value  || value.toString().length != 9)
                {   error="Invalid String Length";
                    return ""
                }
            // Validate format string.
            // It must contain 9 number placeholders.
            var numCharCnt:int = 0;
            for( var i:int = 0; i<formatString.length; i++ )
                {
                    if( formatString.charAt(i) == "#" )
                    {   numCharCnt++;
                    }
                }
            if( numCharCnt != 9 )
            {