////////////////////////////////////////////////////////////////////////////////
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////
package mx.formatters
{
import mx.events.ValidationResultEvent;
import mx.managers.ISystemManager;
import mx.managers.SystemManager;
import mx.validators.PostCodeValidator;
import mx.validators.ValidationResult;
[ResourceBundle("formatters")]
/**
* The PostCodeFormatter class formats a valid postcode
* based on a user set formatString or
* formats property.
*
*
Postcode formats consists of the letters C, N, A and spaces or hyphens *
Country codes one be one or two digits.
* *For example "NNNN" is a four digit numeric postcode, "CCNNNN" is country code * followed by four digits and "AA NNNN" is two letters, followed by a space then * followed by four digits.
* *More than one format can be specified by setting the formats
* property to an array of format strings.
Spaces and hypens will be added if missing to format the postcode correctly.
* *If an error occurs, an empty String is returned and a String that
* describes the error is saved to the error property.
* The error property can have one of the following values:
*
"invalidFormat" means the format constants an invalid
* character."wrongFormat" means the postcode has an invalid format."wrongLength" means the postcode is not a valid length."invalidChar" means the postcode contains an invalid
* character.Fullwidth numbers and letters are supported in postcodes by ignoring character
* width via the flash.globalization.Collator equals method.
The <mx:PostCodeFormatter> tag
* inherits all of the tag attributes of its superclass,
* and adds the following tag attributes:
* <mx:PostCodeFormatter * formatString="NNNNN" * formats="['NNNNN', 'NNNNN-NNNN']" * /> ** * @see mx.validators.PostCodeValidator * * @langversion 3.0 * @playerversion Flash 10.2 * @productversion ApacheFlex 4.8 */ public class PostCodeFormatter extends Formatter { include "../core/Version.as"; //-------------------------------------------------------------------------- // // Constructor // //-------------------------------------------------------------------------- /** * Constructor. * * @langversion 3.0 * @playerversion Flash 10.2 * @productversion ApacheFlex 4.8 */ public function PostCodeFormatter() { super(); } //-------------------------------------------------------------------------- // // Properties // //-------------------------------------------------------------------------- //---------------------------------- // formats //---------------------------------- /** * @private * An array of the postcode formats to check against. */ private var _formats:Array = []; [Inspectable(category = "General", defaultValue = "null")] /** * Format string to format the postcode in. * *
The format string consists of the letters C, N, A and spaces * or hyphens: *
Use for locales where more than one format is required. * eg en_UK
* *See formatString for format of the format
* strings.
formatString
* or formats.
*
* If the value cannot be formatted this method returns an empty String
* and write a description of the error to the error property.
error property.
*
* @langversion 3.0
* @playerversion Flash 10.2
* @productversion ApacheFlex 4.8
*/
override public function format(value:Object):String
{
var postCode:String = value as String;
var formatted:String = "";
var validator:PostCodeValidator = new PostCodeValidator();
var errors:Array;
error = "";
validator.formats = formats;
errors = PostCodeValidator.validatePostCode(validator, postCode, null);
// Valid postcode no need for formatting
if (errors.length == 0)
return postCode ? postCode : "";
// Check and add missing (or convert) padding characters
for each (var format:String in formats)
{
var condensedPostcode:String = condensedFormat(postCode);
var condensedFormat:String = condensedFormat(format);
var char:String;
var length:int = format.length;
var condensedErrors:Array;
validator.format = condensedFormat;
condensedErrors = PostCodeValidator.validatePostCode(validator, condensedPostcode, null);
if (condensedErrors.length == 0)
{
var pos:int = 0;
for (var i:int = 0; i < length; i++)
{
char = format.charAt(i);
if (PostCodeValidator.FORMAT_SPACERS.indexOf(char) >= 0)
formatted += char;
else
formatted += condensedPostcode.charAt(pos++);
}
//TODO may want to return the longest match?
errors = [];
break;
}
}
if (errors.length > 0)
error = (errors[0] as ValidationResult).errorCode;
return formatted;
}
/**
* @private
*
* Take a format or paostCode and strip all spacing characters
* out of it.
*
*/
protected function condensedFormat(postCode:String):String
{
var condensed:String = postCode;
var length:int;
if (postCode)
length = postCode.length;
for (var i:int = 0; i < length; i++)
{
var char:String = PostCodeValidator.FORMAT_SPACERS.charAt(i);
condensed = condensed.split(char).join("");
}
return condensed;
}
}
}