mx.formatters

# Formatter - AS3 Flex

Package | mx.formatters
Class | public class Formatter
Inheritance | Formatter Object
Implements | IFormatter
Subclasses | CurrencyFormatter, DateFormatter, NumberFormatter, PhoneFormatter, ZipCodeFormatter

Language Version: | ActionScript 3.0

Product Version: | Flex 3

Runtime Versions: | Flash Player 9, AIR 1.1

The Formatter class is the base class for all data formatters. Any subclass of Formatter must override the `format()` method.

MXML Syntax Show MXML Syntax
Hide MXML Syntax

The Formatter class defines the following tag attributes, which all of its subclasses inherit:

      <mx:tagname
        Properties
        error=""
      />

View the examples

More examples

Creating a custom formatter

Public Properties
Hide Inherited Public Properties
Show Inherited Public Properties

| Property | Defined By
--- | --- | ---
|  | constructor : Object A reference to the class object or constructor function for a given object instance. | Object
|  | defaultInvalidFormatError : String [static] Error message for an invalid format string specified to the formatter. | Formatter
|  | defaultInvalidValueError : String [static] Error messages for an invalid value specified to the formatter. | Formatter
|  | error : String Description saved by the formatter when an error occurs. | Formatter

Protected Properties

| Property | Defined By
--- | --- | ---
|  | resourceManager : IResourceManager [read-only] A reference to the object which manages all of the application's localized resources. | Formatter

Public Methods
Hide Inherited Public Methods
Show Inherited Public Methods

| Method | Defined By
--- | --- | ---
|  | Formatter() Constructor. | Formatter
|  | format(value:Object):String Formats a value and returns a String containing the new, formatted, value. | Formatter
|  | hasOwnProperty(name:String):Boolean Indicates whether an object has a specified property defined. | Object
|  | isPrototypeOf(theClass:Object):Boolean Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter. | Object
|  | propertyIsEnumerable(name:String):Boolean Indicates whether the specified property exists and is enumerable. | Object
|  | setPropertyIsEnumerable(name:String, isEnum:Boolean = true):void Sets the availability of a dynamic property for loop operations. | Object
|  | toLocaleString():String Returns the string representation of this object, formatted according to locale-specific conventions. | Object
|  | toString():String Returns the string representation of the specified object. | Object
|  | valueOf():Object Returns the primitive value of the specified object. | Object

Protected Methods

| Method | Defined By
--- | --- | ---
|  | resourcesChanged():void This method is called when a Formatter is constructed, and again whenever the ResourceManager dispatches a "change" Event to indicate that the localized resources have changed in some way. | Formatter

Property Detail

### defaultInvalidFormatError

property

`defaultInvalidFormatError:String`

Language Version: | ActionScript 3.0

Product Version: | Flex 3

Runtime Versions: | Flash Player 9, AIR 1.1

Error message for an invalid format string specified to the formatter.

The default value is `"Invalid format".`

Implementation
`public static function get defaultInvalidFormatError():String`
`public static function set defaultInvalidFormatError(value:String):void`

### defaultInvalidValueError

property

`defaultInvalidValueError:String`

Language Version: | ActionScript 3.0

Product Version: | Flex 3

Runtime Versions: | Flash Player 9, AIR 1.1

Error messages for an invalid value specified to the formatter.

The default value is `"Invalid value".`

Implementation
`public static function get defaultInvalidValueError():String`
`public static function set defaultInvalidValueError(value:String):void`

### error

property

`public var error:String`

Language Version: | ActionScript 3.0

Product Version: | Flex 3

Runtime Versions: | Flash Player 9, AIR 1.1

Description saved by the formatter when an error occurs. For the possible values of this property, see the description of each formatter.

Subclasses must set this value in the `format()` method.

### resourceManager

property

`resourceManager:IResourceManager` [read-only]

Language Version: | ActionScript 3.0

Product Version: | Flex 3

Runtime Versions: | Flash Player 9, AIR 1.1

A reference to the object which manages all of the application's localized resources. This is a singleton instance which implements the IResourceManager interface.

This property can be used as the source for data binding. When this property is modified, it dispatches the `unused` event.

Implementation
`protected function get resourceManager():IResourceManager`
Constructor Detail

### Formatter

()
Constructor

`public function Formatter()`

Language Version: | ActionScript 3.0

Product Version: | Flex 3

Runtime Versions: | Flash Player 9, AIR 1.1

Constructor.

Method Detail

### format

()
method

`public function format(value:Object):String`

Language Version: | ActionScript 3.0

Product Version: | Flex 3

Runtime Versions: | Flash Player 9, AIR 1.1

Formats a value and returns a String containing the new, formatted, value. All subclasses must override this method to implement the formatter.

Parameters

| `value:Object` — Value to be formatted.

Returns

| `String` — The formatted string.

### resourcesChanged

()
method

`protected function resourcesChanged():void`

Language Version: | ActionScript 3.0

Product Version: | Flex 3

Runtime Versions: | Flash Player 9, AIR 1.1

This method is called when a Formatter is constructed, and again whenever the ResourceManager dispatches a `"change"` Event to indicate that the localized resources have changed in some way.

This event will be dispatched when you set the ResourceManager's `localeChain` property, when a resource module has finished loading, and when you call the ResourceManager's `update()` method.

Subclasses should override this method and, after calling `super.resourcesChanged()`, do whatever is appropriate in response to having new resource values.

Examples How to use this example
SimpleFormatterExample.mxml
Copy

    <?xml version="1.0" encoding="utf-8"?>
    <!-- Simple example to demonstrate the Formatter class. -->
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
            xmlns:s="library://ns.adobe.com/flex/spark"
            xmlns:mx="library://ns.adobe.com/flex/mx">

        <fx:Script>
            <![CDATA[
                // Event handler to format the input.
                private function Format():void {
                    // The format() method returns the formatted String,
                    // or an empty String if there is an error.
                    var formattedVal:String = numberFormatter.format(inputVal.text);

                    if (formattedVal.length == 0) {
                        // If there is an error, the Format.error property
                        // contains the reason.
                        formattedNumber.text = numberFormatter.error;
                    } else {
                        formattedNumber.text = formattedVal;
                    }
                }
            ]]>
        </fx:Script>

        <fx:Declarations>
            <mx:NumberFormatter id="numberFormatter" />
        </fx:Declarations>

        <s:Panel title="NumberFormatter Example"
                width="75%" height="75%"
                horizontalCenter="0" verticalCenter="0">
            <mx:Form left="10" right="10" top="10" bottom="10">
                <mx:FormItem label="Enter number - a letter is invalid:">
                    <s:TextInput id="inputVal" text="" width="75%"/>
                </mx:FormItem>
                <mx:FormItem label="Formatted number: ">
                    <s:TextInput id="formattedNumber" editable="false" width="75%"/>
                </mx:FormItem>
                <mx:FormItem>
                    <s:Button label="Validate and Format" click="Format();"/>
                </mx:FormItem>
            </mx:Form>
        </s:Panel>

    </s:Application>

© 2015 Adobe Systems Incorporated. All rights reserved.
Mon Apr 10 2017, 12:28 PM -07:00