incubator-flex-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmcl...@apache.org
Subject svn commit: r1299148 - in /incubator/flex/whiteboard/jmclean/validators/src: mx/validators/PostCodeValidator.as tests/PostCodeValidatorTests.as
Date Sat, 10 Mar 2012 03:04:58 GMT
Author: jmclean
Date: Sat Mar 10 03:04:58 2012
New Revision: 1299148

URL: http://svn.apache.org/viewvc?rev=1299148&view=rev
Log:
Added support for Japanese postcode symbol and full width numbers.
Added invalid format error than is generated if the format is empty or contains an invalid
character.

Modified:
    incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as
    incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeValidatorTests.as

Modified: incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as
URL: http://svn.apache.org/viewvc/incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as?rev=1299148&r1=1299147&r2=1299148&view=diff
==============================================================================
--- incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as (original)
+++ incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as Sat
Mar 10 03:04:58 2012
@@ -70,12 +70,16 @@ public class PostCodeValidator extends V
 	
 	public static const ERROR_INVALID_CHAR:String = "invalidChar";
 	public static const ERROR_WRONG_LENGTH:String = "wrongLength";
-	public static const ERROR_WRONG_FORMAT:String = "wrongFormat";
+	public static const ERROR_WRONG_FORMAT:String = "wrongFormat"; 
 	
+	public static const ERROR_INCORRECT_FORMAT:String = "incorrectFormat";
+
 	protected static const FORMAT_NUMBER:String = "N";
 	protected static const FORMAT_LETTER:String = "A";
 	protected static const FORMAT_COUNTRY_CODE:String = "C";
 	protected static const FORMAT_SPACERS:String = " -";
+	
+	protected static const FULL_WIDTH_DIGITS:String = "0123456789";
 
     //--------------------------------------------------------------------------
     //
@@ -83,9 +87,17 @@ public class PostCodeValidator extends V
     //
     //--------------------------------------------------------------------------
 
+	private static function notFormatChar(char:String):Boolean
+	{
+		return FORMAT_SPACERS.indexOf(char) == -1
+			&& char != FORMAT_NUMBER
+			&& char != FORMAT_LETTER
+			&& char != FORMAT_COUNTRY_CODE;
+	}
+	
 	private static function noDecimalDigits(char:String):Boolean
 	{
-		return DECIMAL_DIGITS.indexOf(char) == -1;
+		return DECIMAL_DIGITS.indexOf(char) == -1 && FULL_WIDTH_DIGITS.indexOf(char) ==
-1;
 	}
 	
 	private static function noRomanLetters(char:String):Boolean
@@ -141,6 +153,13 @@ public class PostCodeValidator extends V
 					true, baseField, ERROR_WRONG_FORMAT,
 					validator.wrongFormatError));
 			}
+			
+			if (error.incorrectFormat)
+			{
+				results.push(new ValidationResult(
+					true, baseField, ERROR_INCORRECT_FORMAT,
+					validator.incorrectFormatError));
+			}
 		}
 	}
 	
@@ -151,16 +170,24 @@ public class PostCodeValidator extends V
 		var invalidChar:Boolean;
 		var invalidFormat:Boolean;
 		var wrongLength:Boolean;
+		var incorrectFormat:Boolean;
 		var formatLength:int;
 		var postCodeLength:int;
 		var noChars:int;
 		var countryIndex:int;
 		
-		if (format) {
+		if (format)
+		{
 			formatLength = format.length;
 		}
 		
-		if (postCode) {
+		if (formatLength == 0)
+		{
+			incorrectFormat = true;
+		}
+		
+		if (postCode)
+		{
 			postCodeLength = postCode.length;
 		}
 		
@@ -176,9 +203,17 @@ public class PostCodeValidator extends V
 				char = postCode.charAt(postcodeIndex);
 			}
 			
+			if (notFormatChar(formatChar))
+			{
+				incorrectFormat = true;
+			}
+			
 			if (noDecimalDigits(char) && noRomanLetters(char) && noSpacers(char))
 			{
-				invalidChar = true;
+				if (!countryCode || countryCode.indexOf(char) == -1)
+				{
+					invalidChar = true;
+				}
 			}
 			else if (formatChar == FORMAT_NUMBER && noDecimalDigits(char))
 			{
@@ -205,12 +240,14 @@ public class PostCodeValidator extends V
 		
 		// We want invalid char and invalid format errors to show in preference
 		// so give wrong length errors a higher value
-		if (invalidFormat || invalidChar || wrongLength)
+		if (incorrectFormat || invalidFormat || invalidChar || wrongLength)
 		{
 			return {invalidFormat:invalidFormat,
-					invalidChar:invalidChar, wrongLength:wrongLength,
+					incorrectFormat:incorrectFormat,
+					invalidChar:invalidChar,
+					wrongLength:wrongLength,
 					count:Number(invalidFormat) + Number(invalidChar)
-						+ Number(wrongLength) * 1.5};
+						+ Number(incorrectFormat) + Number(wrongLength) * 1.5};
 		}
 		else
 		{
@@ -319,8 +356,8 @@ public class PostCodeValidator extends V
 	
 	/** 
 	 *  Format of postcode
-	 *  Format consists of CC,N,A and spaces or hyphens
-	 *  CC is country code (required for some postcodes)
+	 *  Format consists of C,N,A and spaces or hyphens
+	 *  CC or C is country code (required for some postcodes)
 	 *	N is a number 0-9
 	 *  A is a letter A-Z or a-z
 	 *
@@ -345,7 +382,7 @@ public class PostCodeValidator extends V
 	 */
 	public function set format(value:String):void
 	{
-		if (value)
+		if (value != null)
 		{
 			_formats = [value];
 		}
@@ -374,7 +411,8 @@ public class PostCodeValidator extends V
 	 */
 	public function set countryCode(value:String):void
 	{
-		if (value == null || value && value.length == 2)
+		// Length is usually 2 character but can use 〒 in Japan
+		if (value == null || value && value.length <= 2)
 		{
 			_countryCode = value;
 		}
@@ -411,10 +449,17 @@ public class PostCodeValidator extends V
 	 * 
 	 * A user supplied method that perform further validation on a postcode.
 	 * 
-	 * The user supplied method should take as a parameter a postcode (String)
-	 * and if the postcode is not valid return an error (String).
-	 *
-	 *  @default null
+	 * The user supplied method should  haver the following signature:
+	 * <code>function validatePostcode(postcode:String):String</code>
+	 * 
+	 * The method is passed the postcode to be validated and should
+	 * return either:
+	 * 1. null indicating the postcode is valid
+	 * 2. A non empty string describing why the postcode is invalid
+	 * 
+	 * The error string will be converted into a ValidationResult when
+	 * doValidation is called by Flex as part of the normal validation
+	 * process.
 	 *  
 	 *  @langversion 3.0
 	 *  @playerversion Flash 10.2
@@ -528,54 +573,103 @@ public class PostCodeValidator extends V
 		}
     }
     
-    //----------------------------------
-    //  wrongFormatError
-    //----------------------------------
-
-    /**
-     *  @private
-     *  Storage for the wrongFormatError property.
-     */
-    private var _wrongFormatError:String;
-    
-    /**
-     *  @private
-     */
-    private var wrongFormatErrorOverride:String;
-
-    [Inspectable(category="Errors", defaultValue="null")]
-
-    /** 
-     *  Error message for an incorrectly formatted postcode.
-     *
-     *  @default "The postcode code must be correctly formatted."
-     *  
+	//----------------------------------
+	//  wrongFormatError
+	//----------------------------------
+	
+	/**
+	 *  @private
+	 *  Storage for the wrongFormatError property.
+	 */
+	private var _wrongFormatError:String;
+	
+	/**
+	 *  @private
+	 */
+	private var wrongFormatErrorOverride:String;
+	
+	[Inspectable(category="Errors", defaultValue="null")]
+	
+	/** 
+	 *  Error message for an incorrectly formatted postcode.
+	 *
+	 *  @default "The postcode code must be correctly formatted."
+	 *  
 	 *  @langversion 3.0
 	 *  @playerversion Flash 10.2
 	 *  @productversion ApacheFlex 4.8
-     */
-    public function get wrongFormatError():String
-    {
+	 */
+	public function get wrongFormatError():String
+	{
 		if (wrongFormatErrorOverride)
 		{
 			return wrongFormatErrorOverride;
 		}
 		
-        return _wrongFormatError;
-    }
-
-    /**
-     *  @private
-     */
-    public function set wrongFormatError(value:String):void
-    {
-        wrongFormatErrorOverride = value;
-
+		return _wrongFormatError;
+	}
+	
+	/**
+	 *  @private
+	 */
+	public function set wrongFormatError(value:String):void
+	{
+		wrongFormatErrorOverride = value;
+		
 		if (!value) {
-        	_wrongFormatError = resourceManager.getString("validators",
+			_wrongFormatError = resourceManager.getString("validators",
 				"wrongFormatPostcodeError");
 		}
-    }
+	}
+
+	//----------------------------------
+	//  incorrectFormatError
+	//----------------------------------
+	
+	/**
+	 *  @private
+	 *  Storage for the incorrectFormatError property.
+	 */
+	private var _incorrectFormatError:String;
+	
+	/**
+	 *  @private
+	 */
+	private var incorrectFormatErrorOverride:String;
+	
+	[Inspectable(category="Errors", defaultValue="null")]
+	
+	/** 
+	 *  Error message for an incorrect format string.
+	 *
+	 *  @default "The postcode format string is incorrect"
+	 *  
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @productversion ApacheFlex 4.8
+	 */
+	public function get incorrectFormatError():String
+	{
+		if (incorrectFormatErrorOverride)
+		{
+			return incorrectFormatErrorOverride;
+		}
+		
+		return _incorrectFormatError;
+	}
+	
+	/**
+	 *  @private
+	 */
+	public function set incorrectFormatError(value:String):void
+	{
+		incorrectFormatErrorOverride = value;
+		
+		if (!value) {
+			_incorrectFormatError = resourceManager.getString("validators",
+				"incorrectFormatPostcodeError");
+		}
+	}
 
     //--------------------------------------------------------------------------
     //
@@ -622,7 +716,7 @@ public class PostCodeValidator extends V
         if (results.length > 0 || ((val.length == 0) && !required))
             return results;
         else
-            return PostCodeValidator.validatePostCode(this, String(value), null);
+            return PostCodeValidator.validatePostCode(this, val, null);
     }
 	
 	
@@ -632,6 +726,21 @@ public class PostCodeValidator extends V
 	//
 	//--------------------------------------------------------------------------
 
+	private function getRegion(locale:String):String {
+		var localeID:LocaleID;
+		
+		if (locale == null)
+		{		
+			var tool:StringTools = new StringTools(LocaleID.DEFAULT);			
+			localeID = new LocaleID(tool.actualLocaleIDName);
+		}
+		else
+		{
+			localeID = new LocaleID(locale);
+		}
+		
+		return localeID.getRegion();
+	}
 	/** 
 	 *  Sets the suggested format for postcodes for a
 	 *  given <code>locale</code>.
@@ -648,19 +757,7 @@ public class PostCodeValidator extends V
 	 *  @productversion ApacheFlex 4.8
 	 */
 	public function suggestFormat(locale:String = null):Array {
-		var localeID:LocaleID;
-		var region:String;
-		
-		if (!locale)
-		{		
-			var tool:StringTools = new StringTools(LocaleID.DEFAULT);			
-			localeID = new LocaleID(tool.actualLocaleIDName);
-		}
-		else
-		{
-			localeID = new LocaleID(locale);
-		}
-		region = localeID.getRegion();
+		var region:String = getRegion(locale);
 			
 		formats = [];
 		
@@ -691,7 +788,7 @@ public class PostCodeValidator extends V
 				formats = ["AN NAA", "ANN NAA", "AAN NAA", "ANA NAA", "AANN NAA", "AANA NAA"];
 				break;
 			case "JP":
-				formats = ["NNNNNNN","NNN-NNNN"];
+				formats = ["NNNNNNN","NNN-NNNN","C NNNNNNN","C NNN-NNNN"];
 				break;
 			case "KR":
 				formats = ["NNNNNN","NNN-NNN"];
@@ -712,6 +809,30 @@ public class PostCodeValidator extends V
 		
 		return formats;
 	}
+	
+	/** 
+	 *  Sets the suggested country code for postcodes for a
+	 *  given <code>locale</code>.
+	 * 
+	 *  If no locale is suplied the default locale is used.
+	 * 
+	 *  Currenly only a limited set of locales are supported.
+	 * 
+	 *  @return The suggested format or an empty array if the
+	 *  locale is not supported. 
+	 *  
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @productversion ApacheFlex 4.8
+	 */
+	public function suggestCountryCode(locale:String = null):String {
+		var region:String = getRegion(locale);
+		
+		if (region == "JP") {
+			countryCode = "〒";
+		}
+		return countryCode;
+	}
 }
 
 }

Modified: incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeValidatorTests.as
URL: http://svn.apache.org/viewvc/incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeValidatorTests.as?rev=1299148&r1=1299147&r2=1299148&view=diff
==============================================================================
--- incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeValidatorTests.as (original)
+++ incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeValidatorTests.as Sat Mar
10 03:04:58 2012
@@ -73,10 +73,37 @@ package tests
 		public function emptyFormat():void {
 			var results:Array;
 			
+			validator.formats = ["NNNN",""];
+			
+			results = PostCodeValidator.validatePostCode(validator, "", null);
+			assertTrue("Incorrect format error", results.length == 1);
+			incorrectFormatError(results);
+			
 			validator.format = "";
 			
 			results = PostCodeValidator.validatePostCode(validator, "", null);
-			assertTrue("No errors", results.length == 0);		
+			assertTrue("Incorrect format error", results.length == 1);
+			incorrectFormatError(results);
+		}
+		
+		[Test]
+		public function invalidFormat():void {
+			var results:Array;
+			
+			validator.format = "ZZZZ";
+			results = PostCodeValidator.validatePostCode(validator, "1234", null);
+			assertTrue("Invalid format", results.length == 1);
+			incorrectFormatError(results);
+			
+			validator.format = "CCAA%NN";
+			results = PostCodeValidator.validatePostCode(validator, "AABBC12", null);
+			assertTrue("Invalid format", results.length == 2);
+			incorrectFormatError(results);			
+			
+			validator.format = "99NN";
+			results = PostCodeValidator.validatePostCode(validator, "9912", null);
+			assertTrue("Invalid format", results.length == 1);
+			incorrectFormatError(results);
 		}
 		
 		[Test]
@@ -110,15 +137,19 @@ package tests
 		}
 		
 		private function invalidFormatError(results:Array):void {
-			assertTrue("Has format error", hasError(results, "wrongFormat"));
+			assertTrue("Has format error", hasError(results, PostCodeValidator.ERROR_WRONG_FORMAT));
+		}
+		
+		private function incorrectFormatError(results:Array):void {
+			assertTrue("Has format error", hasError(results, PostCodeValidator.ERROR_INCORRECT_FORMAT));
 		}
 		
 		private function wrongLengthError(results:Array):void {
-			assertTrue("Has wrong length error", hasError(results, "wrongLength"));
+			assertTrue("Has wrong length error", hasError(results, PostCodeValidator.ERROR_WRONG_LENGTH));
 		}
 		
 		private function invalidCharError(results:Array):void {
-			assertTrue("Has invalid character error", hasError(results, "invalidChar"));
+			assertTrue("Has invalid character error", hasError(results, PostCodeValidator.ERROR_INVALID_CHAR));
 		}
 		
 		[Test]
@@ -130,40 +161,78 @@ package tests
 			results = PostCodeValidator.validatePostCode(validator, "1234", null);
 			assertTrue("No errors", results.length == 0);
 			
+			results = PostCodeValidator.validatePostCode(validator, "1234", null);
+			assertTrue("No errors", results.length == 0);
+			
 			results = PostCodeValidator.validatePostCode(validator, "1-23", null);
 			assertTrue("Invalid Postcode", results.length == 1);
 			invalidFormatError(results);
 			
+			results = PostCodeValidator.validatePostCode(validator, "1-34", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			invalidFormatError(results);
+			
 			results = PostCodeValidator.validatePostCode(validator, "12&3", null);
 			assertTrue("Invalid Postcode", results.length == 1);
 			invalidCharError(results);
 			
+			results = PostCodeValidator.validatePostCode(validator, "12&4", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			invalidCharError(results);
+			
 			results = PostCodeValidator.validatePostCode(validator, "123", null);
 			assertTrue("Invalid Postcode", results.length == 1);
 			wrongLengthError(results);
 			
+			results = PostCodeValidator.validatePostCode(validator, "123", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			wrongLengthError(results);
+			
 			results = PostCodeValidator.validatePostCode(validator, "123456", null);
 			assertTrue("Invalid Postcode", results.length == 1);
 			wrongLengthError(results);
 			
+			results = PostCodeValidator.validatePostCode(validator, "0123456",
null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			wrongLengthError(results);
+			
 			results = PostCodeValidator.validatePostCode(validator, "123D", null);
 			assertTrue("Invalid Postcode", results.length == 1);
 			invalidFormatError(results);
 			
+			results = PostCodeValidator.validatePostCode(validator, "123D", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			invalidFormatError(results);
+			
 			results = PostCodeValidator.validatePostCode(validator, "1234D", null);
 			assertTrue("Invalid Postcode", results.length == 1);
 			wrongLengthError(results);
 			
+			results = PostCodeValidator.validatePostCode(validator, "1234D", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			wrongLengthError(results);
+			
 			results = PostCodeValidator.validatePostCode(validator, "123D4", null);
 			assertTrue("Invalid Postcode", results.length == 2);
 			wrongLengthError(results);
 			invalidFormatError(results);
 			
+			results = PostCodeValidator.validatePostCode(validator, "123D4", null);
+			assertTrue("Invalid Postcode", results.length == 2);
+			wrongLengthError(results);
+			invalidFormatError(results);
+			
 			results = PostCodeValidator.validatePostCode(validator, "1*3D4", null);
 			assertTrue("Invalid Postcode", results.length == 3);
 			wrongLengthError(results);
 			invalidFormatError(results);
 			invalidCharError(results);
+			
+			results = PostCodeValidator.validatePostCode(validator, "1*3D4", null);
+			assertTrue("Invalid Postcode", results.length == 3);
+			wrongLengthError(results);
+			invalidFormatError(results);
+			invalidCharError(results);
 		}
 		
 		[Test]
@@ -327,7 +396,7 @@ package tests
 			var results:Array;
 			var valid:Array = ["2000","3000", "2010", "2462"];
 			
-			validator.format = "NNNN";
+			validator.suggestFormat("en_AU");
 			
 			for each (var postcode:String in valid) {
 				results = PostCodeValidator.validatePostCode(validator, postcode, null);
@@ -340,7 +409,7 @@ package tests
 			var results:Array;
 			var valid:Array = ["10003", "90036", "95124", "94117", "20500"];
 			
-			validator.formats = ["NNNNN", "NNNNN-NNNN"];
+			validator.suggestFormat("en_US");
 			
 			for each (var postcode:String in valid) {
 				results = PostCodeValidator.validatePostCode(validator, postcode, null);
@@ -352,9 +421,8 @@ package tests
 		public function unitedKindomPostcodes():void {
 			var results:Array;
 			var valid:Array = ["M1 1AA", "B33 8TH", "CR2 6XH", "DN55 1PT", "W1A 1HQ", "EC1A 1BB",
"E98 1NW", "SW1A 0PW"];
-
-			//TODO Double check formats correct
-			validator.formats = ["AN NAA", "ANN NAA", "AAN NAA", "ANA NAA", "AANN NAA", "AANA NAA"];
+			
+			validator.suggestFormat("en_UK");
 			
 			for each (var postcode:String in valid) {
 				results = PostCodeValidator.validatePostCode(validator, postcode, null);
@@ -362,12 +430,13 @@ package tests
 			}
 		}
 		
+		
 		[Test]
 		public function canadianPostcodes():void {
 			var results:Array;
 			var valid:Array = ["K0K 2T0", "V6Z 1T0", "B4V 2K4", "H0H 0H0"];
 			
-			validator.format = "ANA NAN";
+			validator.suggestFormat("en_CA");
 			
 			for each (var postcode:String in valid) {
 				results = PostCodeValidator.validatePostCode(validator, postcode, null);
@@ -378,9 +447,10 @@ package tests
 		[Test]
 		public function japanesePostcodes():void {
 			var results:Array;
-			var valid:Array = ["100-0006", "600-8216", "282-0004"];
+			var valid:Array = ["〒 100-0006", "100-0006", "600-8216", "282-0004"];
 			
-			validator.format = "NNN-NNNN";
+			validator.suggestFormat("ja_JP");
+			validator.countryCode = "〒";
 			
 			for each (var postcode:String in valid) {
 				results = PostCodeValidator.validatePostCode(validator, postcode, null);
@@ -498,7 +568,13 @@ package tests
 			assertTrue("Error overridden", validator.wrongLengthError == error);
 			validator.wrongLengthError = null;
 			assertTrue("Error not overridden", validator.wrongLengthError == resourcesUS.content.wrongLengthPostcodeError);
-		}
+
+			assertTrue("Error not overridden", validator.incorrectFormatError == resourcesUS.content.incorrectFormatPostcodeError);
+			validator.incorrectFormatError = error;
+			assertTrue("Error overridden", validator.incorrectFormatError == error);
+			validator.incorrectFormatError = null;
+			assertTrue("Error not overridden", validator.incorrectFormatError == resourcesUS.content.incorrectFormatPostcodeError);
+	}
 		
 		// NOTE: To pass this test project must be compiled with option locale=en_US,en_AU
 		[Test]



Mime
View raw message