flex-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmcl...@apache.org
Subject svn commit: r1299297 - in /incubator/flex/whiteboard/jmclean/validators/src: mx/formatters/PostCodeFormatter.as mx/validators/PostCodeValidator.as tests/PostCodeFormatterTests.as
Date Sat, 10 Mar 2012 23:07:06 GMT
Author: jmclean
Date: Sat Mar 10 23:07:06 2012
New Revision: 1299297

URL: http://svn.apache.org/viewvc?rev=1299297&view=rev
Log:
Added to documentation and fixed a few minor formatting issues.
Changed private static methods to protected so that parent classes can override them if needed.

Changed format static properties to public so that they can be used externally and by the
Formatter class.

Modified:
    incubator/flex/whiteboard/jmclean/validators/src/mx/formatters/PostCodeFormatter.as
    incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as
    incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeFormatterTests.as

Modified: incubator/flex/whiteboard/jmclean/validators/src/mx/formatters/PostCodeFormatter.as
URL: http://svn.apache.org/viewvc/incubator/flex/whiteboard/jmclean/validators/src/mx/formatters/PostCodeFormatter.as?rev=1299297&r1=1299296&r2=1299297&view=diff
==============================================================================
--- incubator/flex/whiteboard/jmclean/validators/src/mx/formatters/PostCodeFormatter.as (original)
+++ incubator/flex/whiteboard/jmclean/validators/src/mx/formatters/PostCodeFormatter.as Sat
Mar 10 23:07:06 2012
@@ -33,8 +33,8 @@ import mx.validators.ValidationResult;
  *  based on a user-supplied <code>formatString</code> or
  *  <code>formats</code> property.
  *  
- *  Postcode format consists of CC,N,A and spaces or hyphens
- *  CC is country code (required for some postcodes)
+ *  Postcode format consists of C,N,A and spaces or hyphens
+ *  C or CC is country code (required for some postcodes)
  *	N is a number 0-9
  *  A is a letter A-Z or a-z
  * 
@@ -45,13 +45,15 @@ import mx.validators.ValidationResult;
  *  More than one format can be specified by setting the <code>formats</code>
  *  property to an array of formats.
  *  
- *  <p>Spaces and hypens will be added if missing to format the postcode.</p>
+ *  <p>Spaces and hypens will be added if missing to format the postcode correctly.</p>
  *
  *  <p>If an error occurs, an empty String is returned and a String that  
  *  describes the error is saved to the <code>error</code> property.  
  *  The <code>error</code> property can have one of the following values:</p>
  *
  *  <ul>
+ *    <li><code>"invalidFormat"</code> means the format constants an invalid
+ *    character.</li>
  *    <li><code>"wrongFormat"</code> means the postcode has an invalid
format.</li>
  *    <li><code>"wrongLength"</code> means the postcode is not a valid
length.</li>
  *    <li><code>"invalidChar"</code> means the postcode contains an invalid
@@ -145,13 +147,13 @@ public class PostCodeFormatter extends F
 	 */
 	public function set formatString(value:String):void
 	{
-		if (value)
+		if (value != null)
 		{
 			_formats = [value];
 		}
 		else
 		{
-			_formats = []
+			_formats = [];
 		}
 	}
 	
@@ -187,16 +189,6 @@ public class PostCodeFormatter extends F
 	//
 	//--------------------------------------------------------------------------
 
-    /**
-	 *  @private    
-     */
-	override protected function resourcesChanged():void
-	{
-		super.resourcesChanged();
-		
-		//TODO may not be required
-	}
-
  	/**
 	 *  Formats the value by using the specified format(s).
 	 *  If the value cannot be formatted this method returns an empty String 
@@ -215,7 +207,6 @@ public class PostCodeFormatter extends F
 	{
 		var postCode:String = value as String;
 		var formatted:String = "";
-		var spacers:String = " -";
 		var validator:PostCodeValidator = new PostCodeValidator();
 		var errors:Array;
 		
@@ -233,25 +224,12 @@ public class PostCodeFormatter extends F
 		// Check and add missing (or convert) padding characters
 		for each (var format:String in formats)
 		{
-			var condensedFormat:String = format;
-			var condensedPostcode:String = postCode;
+			var condensedPostcode:String = condensedFormat(postCode);
+			var condensedFormat:String = condensedFormat(format);
 			var char:String;
-			var length:int = spacers.length;
+			var length:int = format.length;
 			var condensedErrors:Array;
 			
-			for (var i:int = 0; i < length; i++)
-			{
-				char = spacers.charAt(i);
-				if (condensedFormat)
-				{
-					condensedFormat = condensedFormat.split(char).join("");
-				}
-				if (condensedPostcode)
-				{
-					condensedPostcode = condensedPostcode.split(char).join("");
-				}
-			}
-			
 			validator.format = condensedFormat;
 			
 			condensedErrors = PostCodeValidator.validatePostCode(validator, condensedPostcode, null);
@@ -260,31 +238,56 @@ public class PostCodeFormatter extends F
 			{
 				var pos:int = 0;
 				
-				length = format.length;
-				
-				for (i = 0; i < length; i++)
+				for (var i:int = 0; i < length; i++)
 				{
 					char = format.charAt(i);
 					
-					if (spacers.indexOf(char) >= 0)
+					if (PostCodeValidator.FORMAT_SPACERS.indexOf(char) >= 0)
 					{
 						formatted += char;
 					}
-					else {
+					else
+					{
 						formatted += condensedPostcode.charAt(pos++);
 					}
 				}
+				//TODO may want to return the longest match?
+				errors = [];
 				break;
 			}
 		}
 		
-		if (!formatted && errors.length > 0)
+		if (errors.length > 0)
 		{
 			error = (errors[0] as ValidationResult).errorCode;
 		}
 
 		return formatted;
 	}
+	
+	/**
+	 *  @private
+	 * 
+	 *  Take a format or paostCode and strips 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;
+	}
+	
 }
-
 }

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=1299297&r1=1299296&r2=1299297&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 23:07:06 2012
@@ -32,8 +32,8 @@ import mx.resources.ResourceManager;
  *  The PostCodeValidator class validates that a String
  *  has the correct length and format for a post code.
  * 
- *  Postcode format consists of CC,N,A and spaces or hyphens
- *  CC is country code (required for some postcodes)
+ *  Postcode 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
  * 
@@ -41,6 +41,8 @@ import mx.resources.ResourceManager;
  *  followed by four digits and "AA NNNN" is two letters, followed by a space
  *  followed by four digits.
  * 
+ *  Country codes one be one or two digits.
+ * 
  *  More than one format can be specified by setting the <code>formats</code>
  *  property to an array of formats.
  *  
@@ -52,10 +54,13 @@ import mx.resources.ResourceManager;
  *  
  *  <pre>
  *  &lt;mx:PostCodeValidator
+ *    countryCode="CC"
  *    format="NNNNN"
  *    formats="['NNNNN', 'NNNNN-NNNN']"
- *    invalidCharError="The post code contains invalid characters." 
- *    wrongLengthError="The post code is the wrong length" 
+ *    wrongFromatError="The postcode code must be correctly formatted."
+ *    invalidFormatError="The postcode format string is incorrect."
+ *    invalidCharError="The postcode contains invalid characters." 
+ *    wrongLengthError="The postcode is the wrong length." 
  *  /&gt;
  *  </pre>
  *  
@@ -74,10 +79,10 @@ public class PostCodeValidator extends V
 	
 	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 = " -";
+	public static const FORMAT_NUMBER:String = "N";
+	public static const FORMAT_LETTER:String = "A";
+	public static const FORMAT_COUNTRY_CODE:String = "C";
+	public static const FORMAT_SPACERS:String = " -";
 	
 	protected static const FULL_WIDTH_DIGITS:String = "0123456789";
 
@@ -87,7 +92,14 @@ public class PostCodeValidator extends V
     //
     //--------------------------------------------------------------------------
 
-	private static function notFormatChar(char:String):Boolean
+	/**
+	 *  @private
+	 * 
+	 *  @param char to check
+	 *  @return true if the char is not a valid format character
+	 *
+	 */
+	protected static function notFormatChar(char:String):Boolean
 	{
 		return FORMAT_SPACERS.indexOf(char) == -1
 			&& char != FORMAT_NUMBER
@@ -95,22 +107,52 @@ public class PostCodeValidator extends V
 			&& char != FORMAT_COUNTRY_CODE;
 	}
 	
-	private static function noDecimalDigits(char:String):Boolean
+	/**
+	 *  @private
+	 * 
+	 *  @param char to check
+	 *  @return true if the char is not a valid digit
+	 *
+	 */
+	protected static function noDecimalDigits(char:String):Boolean
 	{
-		return DECIMAL_DIGITS.indexOf(char) == -1 && FULL_WIDTH_DIGITS.indexOf(char) ==
-1;
+		return DECIMAL_DIGITS.indexOf(char) == -1
+			&& FULL_WIDTH_DIGITS.indexOf(char) == -1;
 	}
 	
-	private static function noRomanLetters(char:String):Boolean
+	/**
+	 *  @private
+	 * 
+	 *  @param char to check
+	 *  @return true if the char is not a valid letter
+	 *
+	 */
+	protected static function noRomanLetters(char:String):Boolean
 	{
 		return ROMAN_LETTERS.indexOf(char) == -1;
 	}
 	
-	private static function noSpacers(char:String):Boolean
+	/**
+	 *  @private
+	 * 
+	 *  @param char to check
+	 *  @return true if the char is not a valid spacer
+	 *
+	 */
+	protected static function noSpacers(char:String):Boolean
 	{
 		return FORMAT_SPACERS.indexOf(char) == -1;
 	}
-	
-	private static function userValidationResults(validator:PostCodeValidator,
+
+	/**
+	 *  @private
+	 * 
+	 *  A wrong format ValidationResult is added to the results array
+	 *  if the extraValidation user supplied function returns an errors.
+	 *  ie There is a user defined issue with the supplied postCode.
+	 *
+	 */
+	protected static function userValidationResults(validator:PostCodeValidator,
 												 baseField:String,
 												 postCode:String,
 												 results:Array):void
@@ -127,12 +169,28 @@ public class PostCodeValidator extends V
 		}
 	}
 	
-	private static function errorValidationResults(validator:PostCodeValidator,
+	/**
+	 *  @private
+	 * 
+	 *  Based on flags in the error object new ValidationResults are
+	 *  added the the results array.
+	 * 
+	 *  Note that the only first ValidationResult by default is shown
+	 *  in validation errors.
+	 *
+	 */
+	protected static function errorValidationResults(validator:PostCodeValidator,
 												  baseField:String,
 												  error:Object,
 												  results:Array):void
 	{
-		if (error) {			
+		if (error) {
+			if (error.incorrectFormat)
+			{
+				results.push(new ValidationResult(
+					true, baseField, ERROR_INCORRECT_FORMAT,
+					validator.incorrectFormatError));
+			}
 			if (error.invalidChar)
 			{
 				results.push(new ValidationResult(
@@ -153,17 +211,19 @@ 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));
-			}
 		}
 	}
 	
-	private static function checkPostCodeFormat(postCode:String,
+	/**
+	 *  @private
+	 * 
+	 *  Check thats a postCode is valid and matches a certain format.
+	 * 
+	 *  @return An error object containing flags for each type of error
+	 *  and an indication of invalidness (used to later sort errors)
+	 *
+	 */
+	protected static function checkPostCodeFormat(postCode:String,
 											   format:String,
 											   countryCode:String):Object
 	{
@@ -246,7 +306,7 @@ public class PostCodeValidator extends V
 					incorrectFormat:incorrectFormat,
 					invalidChar:invalidChar,
 					wrongLength:wrongLength,
-					count:Number(invalidFormat) + Number(invalidChar)
+					invalidness:Number(invalidFormat) + Number(invalidChar)
 						+ Number(incorrectFormat) + Number(wrongLength) * 1.5};
 		}
 		else
@@ -308,7 +368,7 @@ public class PostCodeValidator extends V
 		}
 
 		// return result with least number of errors
-		errors.sortOn("count", Array.NUMERIC);
+		errors.sortOn("invalidness", Array.NUMERIC);
 		
 		userValidationResults(validator, baseField, postCode, results);
 
@@ -393,7 +453,7 @@ public class PostCodeValidator extends V
 	}
 
 	/** 
-	 *  Optional 2 letter country code in postcode format
+	 *  Optional 1 or 2 letter country code in postcode format
 	 *
 	 *  @default null
 	 *  
@@ -419,11 +479,10 @@ public class PostCodeValidator extends V
 	}
 
 	/** 
-	 *  Formats of postcode
+	 *  Multiple postcode formats
 	 *  Sets an array of valid formats, use for locales
-	 *  where more than one format is required. eg en_UK
-	 *  See format for format.
-	 * 
+	 *  where more than one poscode format exists. eg en_UK
+	 *  See format for details of teh postcode format.
 	 *
 	 *  @default []
 	 *  
@@ -516,7 +575,8 @@ public class PostCodeValidator extends V
     {
         invalidCharErrorOverride = value;
 
-        if (!value) {
+        if (!value)
+		{
 			_invalidCharError =  resourceManager.getString("validators",
 				"invalidCharPostcodeError");
 		}
@@ -616,7 +676,8 @@ public class PostCodeValidator extends V
 	{
 		wrongFormatErrorOverride = value;
 		
-		if (!value) {
+		if (!value)
+		{
 			_wrongFormatError = resourceManager.getString("validators",
 				"wrongFormatPostcodeError");
 		}
@@ -665,7 +726,8 @@ public class PostCodeValidator extends V
 	{
 		incorrectFormatErrorOverride = value;
 		
-		if (!value) {
+		if (!value)
+		{
 			_incorrectFormatError = resourceManager.getString("validators",
 				"incorrectFormatPostcodeError");
 		}
@@ -687,6 +749,7 @@ public class PostCodeValidator extends V
         invalidCharError = invalidCharErrorOverride;
         wrongLengthError = wrongLengthErrorOverride;
         wrongFormatError = wrongFormatErrorOverride;    
+		incorrectFormatError = incorrectFormatErrorOverride;
     }
 
     /**
@@ -722,11 +785,20 @@ public class PostCodeValidator extends V
 	
 	//--------------------------------------------------------------------------
 	//
-	//  methods
+	//  Methods
 	//
 	//--------------------------------------------------------------------------
 
-	private function getRegion(locale:String):String {
+	/**
+	 *  @private
+	 *  Returns the region (usually country) from a locale string.
+	 *  If no loacle is provided the default locale is used.
+	 * 
+	 *  @param locale locale to obtain region from.
+	 *  @return Region string.
+	 *
+	 */
+	protected function getRegion(locale:String):String {
 		var localeID:LocaleID;
 		
 		if (locale == null)
@@ -741,6 +813,7 @@ public class PostCodeValidator extends V
 		
 		return localeID.getRegion();
 	}
+	
 	/** 
 	 *  Sets the suggested format for postcodes for a
 	 *  given <code>locale</code>.
@@ -749,6 +822,8 @@ public class PostCodeValidator extends V
 	 * 
 	 *  Currenly only a limited set of locales are supported.
 	 * 
+	 *  @param locale locale to obtain format for.
+	 * 
 	 *  @return The suggested format or an empty array if the
 	 *  locale is not supported. 
 	 *  
@@ -761,7 +836,8 @@ public class PostCodeValidator extends V
 			
 		formats = [];
 		
-		switch (region) {
+		switch (region)
+		{
 			case "AU":
 			case "DK":
 			case "NO":
@@ -818,7 +894,9 @@ public class PostCodeValidator extends V
 	 * 
 	 *  Currenly only a limited set of locales are supported.
 	 * 
-	 *  @return The suggested format or an empty array if the
+	 *  @param locale locale to obtain country code for.
+	 * 
+	 *  @return The suggested code or an null string if the
 	 *  locale is not supported. 
 	 *  
 	 *  @langversion 3.0
@@ -828,9 +906,11 @@ public class PostCodeValidator extends V
 	public function suggestCountryCode(locale:String = null):String {
 		var region:String = getRegion(locale);
 		
-		if (region == "JP") {
+		if (region == "JP")
+		{
 			countryCode = "〒";
 		}
+		
 		return countryCode;
 	}
 }

Modified: incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeFormatterTests.as
URL: http://svn.apache.org/viewvc/incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeFormatterTests.as?rev=1299297&r1=1299296&r2=1299297&view=diff
==============================================================================
--- incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeFormatterTests.as (original)
+++ incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeFormatterTests.as Sat Mar
10 23:07:06 2012
@@ -1,6 +1,7 @@
 package tests
 {
 	import mx.formatters.PostCodeFormatter;
+	import mx.validators.PostCodeValidator;
 	
 	import org.flexunit.asserts.assertTrue;
 
@@ -42,15 +43,39 @@ package tests
 		}
 		
 		private function invalidFormatError(error:String):void {
-			assertTrue("Has format error", error == "wrongFormat");
+			assertTrue("Has format error", error == PostCodeValidator.ERROR_WRONG_FORMAT);
+		}
+		
+		private function incorrectFormatError(error:String):void {
+			assertTrue("Has incorrect format", error == PostCodeValidator.ERROR_INCORRECT_FORMAT);
 		}
 		
 		private function wrongLengthError(error:String):void {
-			assertTrue("Has wrong length error", error == "wrongLength");
+			assertTrue("Has wrong length error", error == PostCodeValidator.ERROR_WRONG_LENGTH);
 		}
 		
 		private function invalidCharError(error:String):void {
-			assertTrue("Has invalid character error", error == "invalidChar");
+			assertTrue("Has invalid character error", error == PostCodeValidator.ERROR_INVALID_CHAR);
+		}
+		
+		[Test]
+		public function blankFormat():void {
+			formatter.formatString = "";
+			
+			assertTrue("Format is incorrect", formatter.format("1234") == "");
+			incorrectFormatError(formatter.error);	
+		}
+		
+		[Test]
+		public function incorrectFormats():void
+		{
+			formatter.formatString = "NZZN";
+			
+			assertTrue("Format is incorrect", formatter.format("1234") == "");
+			incorrectFormatError(formatter.error);
+			
+			assertTrue("Format is incorrect", formatter.format("ABCD") == "");
+			incorrectFormatError(formatter.error);
 		}
 
 		



Mime
View raw message