incubator-flex-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmcl...@apache.org
Subject svn commit: r1297838 - in /incubator/flex/whiteboard/jmclean/validators/src: mx/validators/PostCodeValidator.as tests/PostCodeValidatorTests.as
Date Wed, 07 Mar 2012 02:14:54 GMT
Author: jmclean
Date: Wed Mar  7 02:14:54 2012
New Revision: 1297838

URL: http://svn.apache.org/viewvc?rev=1297838&view=rev
Log:
Added suggestFormat method (which provides a suggested postcode format based on user supplied
or current locale) Only a limited number of locales are supported (current SDK locales and
CA)

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=1297838&r1=1297837&r2=1297838&view=diff
==============================================================================
--- incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as (original)
+++ incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as Wed
Mar  7 02:14:54 2012
@@ -20,8 +20,11 @@
 package mx.validators
 {
 
+import flash.globalization.LocaleID;
+import flash.globalization.StringTools;
+
 import mx.resources.IResourceManager;
-import mx.resources.ResourceManager;
+import mx.resources.ResourceManager;
 
 [ResourceBundle("validators")]
 
@@ -177,7 +180,7 @@ public class PostCodeValidator extends V
 				count:Number(invalidFormat) + Number(invalidChar) + Number(wrongLength)*1.5})
 		}
 		
-		if (validator && validator.extraValidation)
+		if (validator && validator.extraValidation != null)
 		{
 			var extraError:String = validator.extraValidation(postCode);
 			
@@ -560,6 +563,87 @@ public class PostCodeValidator extends V
         else
             return PostCodeValidator.validatePostCode(this, String(value), null);
     }
+
+	/** 
+	 *  Sets the suggested format 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 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();
+			
+		formats = [];
+		
+		switch (region) {
+			case "AU":
+			case "DK":
+			case "NO":
+				formats = ["NNNN"];
+				break;
+			case "BR":
+				formats = ["NNNNN-NNN"];
+				break;	
+			case "CN":
+			case "DE":
+				formats = ["NNNNNN"];
+				break;	
+			case "CA":
+				formats = ["ANA NAN"];
+				break;
+			case "ES":
+			case "FI":
+			case "FR":
+			case "IT":
+			case "TW":
+				formats = ["NNNNN"];
+				break;	
+			case "GB":
+				formats = ["AN NAA", "ANN NAA", "AAN NAA", "ANA NAA", "AANN NAA", "AANA NAA"];
+				break;
+			case "JP":
+				formats = ["NNNNNNN","NNN-NNNN"];
+				break;
+			case "KR":
+				formats = ["NNNNNN","NNN-NNN"];
+				break;
+			case "NL":
+				formats = ["NNNN AA"];
+				break;
+			case "RU":
+				formats = ["NNNNNN"];
+				break;	
+			case "SE":
+				formats = ["NNNNN","NNN NN"];
+				break;			
+			case "US":
+				formats = ["NNNNN", "NNNNN-NNNN"];
+				break;
+		}
+		
+		return formats;
+	}
 }
 
 }

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=1297838&r1=1297837&r2=1297838&view=diff
==============================================================================
--- incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeValidatorTests.as (original)
+++ incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeValidatorTests.as Wed Mar
 7 02:14:54 2012
@@ -514,6 +514,34 @@ package tests
 			assertTrue("AU Error", validator.wrongFormatError == resourcesAU.content.wrongFormatPostcodeError);
 			assertTrue("AU Error", validator.wrongLengthError == resourcesAU.content.wrongLengthPostcodeError);
 		}
+		
+		[Test]
+		public function suggestedFormat():void {
+			validator.suggestFormat("en_AU");
+			assertTrue("Australian format", validator.format == "NNNN");
+			
+			validator.suggestFormat("en-CA");
+			assertTrue("Australian format", validator.format == "ANA NAN");
+			
+			validator.suggestFormat("en_US");
+			assertTrue("US format", validator.formats[0] == "NNNNN");
+			assertTrue("US format", validator.formats[1] == "NNNNN-NNNN");
+			
+			validator.suggestFormat("it_IT");
+			assertTrue("IT format", validator.format == "NNNNN");
+			
+			validator.suggestFormat("fr_CA");
+			assertTrue("CA format", validator.format == "ANA NAN");
+			validator.suggestFormat("en_CA");
+			assertTrue("CA format", validator.format == "ANA NAN");
+			
+			validator.suggestFormat("en_NZ"); // not known
+			assertTrue("NZ format not known", validator.format == null);
+			
+			// may set format (based on user locale) to anything just check no RTE
+			// and returns something
+			assertTrue("User locale format", validator.suggestFormat() != null)
+		}
 
 	}
 }
\ No newline at end of file



Mime
View raw message