incubator-flex-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmcl...@apache.org
Subject svn commit: r1295396 - in /incubator/flex/whiteboard/jmclean/validators: ./ src/ src/mx/ src/mx/core/ src/mx/core/Version.as src/mx/validators/ src/mx/validators/PostCodeValidator.as src/tests/ src/tests/PostCodeValidatorTests.as
Date Thu, 01 Mar 2012 03:07:07 GMT
Author: jmclean
Date: Thu Mar  1 03:07:05 2012
New Revision: 1295396

URL: http://svn.apache.org/viewvc?rev=1295396&view=rev
Log:
First draft of a mx style generic post code validator 

Added:
    incubator/flex/whiteboard/jmclean/validators/   (with props)
    incubator/flex/whiteboard/jmclean/validators/src/   (with props)
    incubator/flex/whiteboard/jmclean/validators/src/mx/
    incubator/flex/whiteboard/jmclean/validators/src/mx/core/
    incubator/flex/whiteboard/jmclean/validators/src/mx/core/Version.as
    incubator/flex/whiteboard/jmclean/validators/src/mx/validators/
    incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as
    incubator/flex/whiteboard/jmclean/validators/src/tests/
    incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeValidatorTests.as

Propchange: incubator/flex/whiteboard/jmclean/validators/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Thu Mar  1 03:07:05 2012
@@ -0,0 +1,3 @@
+.actionScriptProperties
+.flexProperties
+.project

Propchange: incubator/flex/whiteboard/jmclean/validators/src/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Thu Mar  1 03:07:05 2012
@@ -0,0 +1,3 @@
+FlexUnitApplication.mxml
+FlexUnitCompilerApplication.mxml
+Validotors.mxml

Added: incubator/flex/whiteboard/jmclean/validators/src/mx/core/Version.as
URL: http://svn.apache.org/viewvc/incubator/flex/whiteboard/jmclean/validators/src/mx/core/Version.as?rev=1295396&view=auto
==============================================================================
--- incubator/flex/whiteboard/jmclean/validators/src/mx/core/Version.as (added)
+++ incubator/flex/whiteboard/jmclean/validators/src/mx/core/Version.as Thu Mar  1 03:07:05
2012
@@ -0,0 +1,26 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  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.
+//
+////////////////////////////////////////////////////////////////////////////////
+
+import mx.core.mx_internal;
+
+/**
+ *  @private
+ *  Version string for this class.
+ */
+mx_internal static const VERSION:String = "4.8.0.0";

Added: 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=1295396&view=auto
==============================================================================
--- incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as (added)
+++ incubator/flex/whiteboard/jmclean/validators/src/mx/validators/PostCodeValidator.as Thu
Mar  1 03:07:05 2012
@@ -0,0 +1,473 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  ADOBE SYSTEMS INCORPORATED
+//  Copyright 2003-2007 Adobe Systems Incorporated
+//  All Rights Reserved.
+//
+//  NOTICE: Adobe permits you to use, modify, and distribute this file
+//  in accordance with the terms of the license agreement accompanying it.
+//
+////////////////////////////////////////////////////////////////////////////////
+
+package mx.validators
+{
+
+import mx.resources.IResourceManager;
+import mx.resources.ResourceManager;
+
+[ResourceBundle("validators")]
+
+/**
+ *  The PostCodeValidator class validates that a String
+ *  has the correct length and format for a post code.
+ *  
+ *  @mxml
+ *
+ *  <p>The <code>&lt;mx:PostCodeValidator&gt;</code> tag
+ *  inherits all of the tag attributes of its superclass,
+ *  and adds the following tag attributes:</p>
+ *  
+ *  <pre>
+ *  &lt;mx:PostCodeValidator
+ *    format="NNNN"
+ *    invalidCharError="The post code contains invalid characters." 
+ *    wrongLengthError="The post code is the wrong length" 
+ *  /&gt;
+ *  </pre>
+ *  
+ *  
+ *  @langversion 3.0
+ *  @playerversion Flash 10.2
+ *  @productversion ApacheFlex 4.8
+ */
+public class PostCodeValidator extends Validator
+{
+    include "../core/Version.as";
+
+    //--------------------------------------------------------------------------
+    //
+    //  Class methods
+    //
+    //--------------------------------------------------------------------------
+
+    /**
+     *  Convenience method for calling a validator.
+     *  Each of the standard Flex validators has a similar convenience method.
+     *
+     *  @param validator The PostCodeValidator instance.
+     *
+     *  @param value A field to validate.
+     *
+     *  @param baseField Text representation of the subfield
+     *  specified in the <code>value</code> parameter.
+     *  For example, if the <code>value</code> parameter specifies value.postCode,
+     *  the <code>baseField</code> value is <code>"postCode"</code>.
+     *
+     *  @return An Array of ValidationResult objects, with one ValidationResult 
+     *  object for each field examined by the validator. 
+     *
+     *  @see mx.validators.ValidationResult
+     *
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @productversion ApacheFlex 4.8
+     */
+    public static function validatePostCode(validator:PostCodeValidator,
+                                           value:Object,
+                                           baseField:String):Array
+    {
+        var results:Array = [];
+    
+        // Resource-backed properties of the validator.
+        var resourceManager:IResourceManager = ResourceManager.getInstance();
+
+        var postCode:String = String(value);
+        var length:int = postCode.length;
+		var noformats:int = _formats.length;
+		var validLetters:String = "CAN";
+		var spacers:String = " -";
+		
+		var wrongLength:Boolean;
+		var invalidChar:Boolean;
+		var allInvalidFormat:Boolean;
+		var invalidFormat:Boolean;
+		
+		var digit:int;
+		
+		for (var f:int = 0; f < noformats; f++)
+		{	
+			invalidFormat = false;
+			digit = 0;
+			
+			for (var i:int = 0; i < length; i++)
+			{
+				var char:String = postCode.charAt(i);
+				var formatChar:String = _formats[f].charAt(i);
+				
+				if (DECIMAL_DIGITS.indexOf(char) == -1
+					&& ROMAN_LETTERS.indexOf(char) && spacers.indexOf(char) != -1)
+				{
+					invalidChar = true;
+				}
+				else if (formatChar == "N" && DECIMAL_DIGITS.indexOf(char) == -1)
+				{
+					invalidFormat = true;
+				}
+				else if (formatChar == "A" && ROMAN_LETTERS.indexOf(char) == -1)
+				{
+					invalidFormat = true;
+				}
+				else if (formatChar == "C")
+				{
+					if (digit >= 2 || !_countryCode || char != _countryCode[digit])
+					{
+						invalidFormat = true;
+					}
+					digit++;
+				}
+			}
+			
+			wrongLength = wrongLength || (length != _formats[f].length);
+			allInvalidFormat = allInvalidFormat && invalidFormat;
+		}
+		
+		if (invalidChar)
+		{
+			results.push(new ValidationResult(
+				true, baseField, "invalidChar",
+				validator.invalidCharError));
+		}
+        
+        if (wrongLength)
+        {
+            results.push(new ValidationResult(
+                true, baseField, "wrongLength",
+                validator.wrongLengthError));
+        }
+        
+		if (allInvalidFormat)
+		{
+      	  results.push(new ValidationResult(
+                true, baseField, "wrongFormat",
+                validator.wrongFormatError));
+		}
+
+        return results;
+    }
+    
+    //--------------------------------------------------------------------------
+    //
+    //  Constructor
+    //
+    //--------------------------------------------------------------------------
+
+    /**
+     *  Constructor.
+     *  
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @productversion ApacheFlex 4.8
+     */
+    public function PostCodeValidator()
+    {
+        super();
+    }
+
+    //--------------------------------------------------------------------------
+    //
+    //  Properties
+    //
+    //--------------------------------------------------------------------------
+	
+	/** 
+	 *  @private
+	 *  The two letter country code used in some postcode formats
+	 */
+	protected static var _countryCode:String;
+	
+	/**
+	 *  @private
+	 *  An array of the postcode formats to check against.
+	 */
+	protected static var _formats:Array = [];
+	
+	/** 
+	 *  Format of postcode
+	 *  Format constist of CC,N,A and space
+	 *  CC is country code (rquired for some postcodes)
+	 *	N is a number 0-9
+	 *  A is a letter A-Z or a-z
+	 *
+	 *  @default null
+	 *  
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @productversion ApacheFlex 4.8
+	 */
+	public function get format():String
+	{
+		if (_formats && _formats.length == 1)
+		{
+			return _formats[0];
+		}
+		
+		return null;
+	}
+	
+	/**
+	 *  @private
+	 */
+	public function set format(value:String):void
+	{
+		if (value)
+		{
+			_formats = [value];
+		}
+		else
+		{
+			_formats = []
+		}
+	}
+
+	/** 
+	 *  Optional 2 letter country code in postcode format
+	 *
+	 *  @default null
+	 *  
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @productversion ApacheFlex 4.8
+	 */
+	public function get countryCode():String
+	{
+		return _countryCode;
+	}
+	
+	/**
+	 *  @private
+	 */
+	public function set countryCode(value:String):void
+	{
+		if (value && value.length == 2)
+		{
+			_countryCode = value;
+		}
+	}
+
+	/** 
+	 *  Formats of postcode
+	 *  Sets an array of valid formats, use for locales
+	 *  where more than one format is required. eg en_UK
+	 *  See format for format.
+	 * 
+	 *
+	 *  @default []
+	 *  
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @productversion ApacheFlex 4.8
+	 */
+	public function get formats():Array
+	{
+		return _formats;
+	}
+	
+	/**
+	 *  @private
+	 */
+	public function set formats(value:Array):void
+	{
+		_formats = value;
+	}
+
+    //--------------------------------------------------------------------------
+    //
+    //  Properties: Errors
+    //
+    //--------------------------------------------------------------------------
+
+    //----------------------------------
+    //  invalidCharError
+    //----------------------------------
+
+    /**
+     *  @private
+     *  Storage for the invalidCharError property.
+     */
+    private var _invalidCharError:String;
+    
+    /**
+     *  @private
+     */
+    private var invalidCharErrorOverride:String;
+
+    [Inspectable(category="Errors", defaultValue="null")]
+
+    /** 
+     *  Error message when the post code contains invalid characters.
+     *
+     *  @default "The postcode code contains invalid characters."
+     *  
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @productversion ApacheFlex 4.8
+     */
+    public function get invalidCharError():String
+    {
+        return _invalidCharError;
+    }
+
+    /**
+     *  @private
+     */
+    public function set invalidCharError(value:String):void
+    {
+        invalidCharErrorOverride = value;
+
+        if (!value) {
+			_invalidCharError =  resourceManager.getString("validators",
+				"invalidCharPostcodeError");
+		}
+    }
+
+
+    //----------------------------------
+    //  wrongLengthError
+    //----------------------------------
+    
+    /**
+     *  @private
+     *  Storage for the wrongLengthError property.
+     */
+    private var _wrongLengthError:String;
+    
+    /**
+     *  @private
+     */
+    private var wrongLengthErrorOverride:String;
+
+    [Inspectable(category="Errors", defaultValue="null")]
+
+    /** 
+     *  Error message for an invalid postcode.
+     *
+     *  @default "The postcode is invalid."
+     *  
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @productversion ApacheFlex 4.8
+     */
+    public function get wrongLengthError():String
+    {
+        return _wrongLengthError;
+    }
+
+    /**
+     *  @private
+     */
+    public function set wrongLengthError(value:String):void
+    {
+        wrongLengthErrorOverride = value;
+
+		if (!value)
+		{
+        	_wrongLengthError = resourceManager.getString("validators",
+				"wrongLengthPostcodeError");
+		}
+    }
+    
+    //----------------------------------
+    //  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
+    {
+        return _wrongFormatError;
+    }
+
+    /**
+     *  @private
+     */
+    public function set wrongFormatError(value:String):void
+    {
+        wrongFormatErrorOverride = value;
+
+		if (!value) {
+        	_wrongFormatError = resourceManager.getString("validators",
+				"wrongFormatPostcodeError");
+		}
+    }
+
+    //--------------------------------------------------------------------------
+    //
+    //  Overridden methods
+    //
+    //--------------------------------------------------------------------------
+   
+    /**
+     *  @private    
+     */
+    override protected function resourcesChanged():void
+    {
+        super.resourcesChanged();
+
+        invalidCharError = invalidCharErrorOverride;
+        wrongLengthError = wrongLengthErrorOverride;
+        wrongFormatError = wrongFormatErrorOverride;    
+    }
+
+    /**
+     *  Override of the base class <code>doValidation()</code> method
+     *  to validate a postcode.
+     *
+     *  <p>You do not call this method directly;
+     *  Flex calls it as part of performing a validation.
+     *  If you create a custom Validator class, you must implement this method. </p>
+     *
+     *  @param value Object to validate.
+     *
+     *  @return An Array of ValidationResult objects, with one ValidationResult 
+     *  object for each field examined by the validator. 
+     *  
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @productversion ApacheFlex 4.8
+     */
+    override protected function doValidation(value:Object):Array
+    {
+        var results:Array = super.doValidation(value);
+        
+        // Return if there are errors
+        // or if the required property is set to false and length is 0.
+        var val:String = value ? String(value) : "";
+        if (results.length > 0 || ((val.length == 0) && !required))
+            return results;
+        else
+            return PostCodeValidator.validatePostCode(this, value, null);
+    }
+}
+
+}
+

Added: 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=1295396&view=auto
==============================================================================
--- incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeValidatorTests.as (added)
+++ incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeValidatorTests.as Thu Mar
 1 03:07:05 2012
@@ -0,0 +1,186 @@
+package tests
+{
+	import mx.validators.PostCodeValidator;
+	
+	import org.flexunit.asserts.assertTrue;
+
+	public class PostCodeValidatorTests
+	{		
+		public var validator:PostCodeValidator;
+		
+		[Before]
+		public function setUp():void
+		{
+			validator = new PostCodeValidator();
+
+			// Currently stored in a static variables as the the doValidation method is static
+			validator.format = null;
+			validator.countryCode = null;
+		}
+		
+		[After]
+		public function tearDown():void
+		{
+			validator = null;
+		}
+
+		[Test]
+		public function initial():void
+		{
+			assertTrue("Format is null", validator.format == null);
+			assertTrue("Formats is empty array", validator.formats.length == 0);
+			assertTrue("Country code is null", validator.countryCode == null);
+		}
+		
+		[Test]
+		public function setFormats():void {
+			validator.format = "NNNN";
+			assertTrue("Format is correct", validator.format = "NNNN");
+			assertTrue("Formats length is correct", validator.formats.length = 1);
+			assertTrue("Formats is correct", validator.formats[0] = "NNNN");
+			
+			validator.formats = ["NNNN", "NNNNNN"];
+			assertTrue("Format is null", validator.format == null);
+			assertTrue("Formats length is correct", validator.formats.length = 2);
+			assertTrue("First format is correct", validator.formats[0] = "NNNN");
+			assertTrue("Second format is correct", validator.formats[1] = "NNNNNN");
+		}
+		
+		[Test]
+		public function countryCode():void {
+			validator.countryCode = "AU";
+			assertTrue("Country code is correct", validator.countryCode = "AU");
+		}
+		
+		
+		[Test]
+		public function fixedNumericPostcode():void {
+			var results:Array;
+			
+			validator.format = "NNNN";
+			
+			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);
+			
+			results = PostCodeValidator.validatePostCode(validator, "123", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			
+			results = PostCodeValidator.validatePostCode(validator, "123456", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			
+			results = PostCodeValidator.validatePostCode(validator, "123D", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			
+			results = PostCodeValidator.validatePostCode(validator, "1234D", null);
+			assertTrue("Invalid Postcode", results.length == 2);		
+		}
+		
+		[Test]
+		public function multpleNumericPostcodes():void {
+			var results:Array;
+			
+			validator.formats = ["NNNN", "NNNNNN"];
+			
+			results = PostCodeValidator.validatePostCode(validator, "1234", null);
+			assertTrue("No errors", results.length == 0);
+			
+			results = PostCodeValidator.validatePostCode(validator, "123456", null);
+			assertTrue("No errors", results.length == 0);
+			
+			results = PostCodeValidator.validatePostCode(validator, "1-23", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			
+			results = PostCodeValidator.validatePostCode(validator, "123", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			
+			results = PostCodeValidator.validatePostCode(validator, "12345", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			
+			results = PostCodeValidator.validatePostCode(validator, "1234567", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			
+			results = PostCodeValidator.validatePostCode(validator, "123D", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			
+			results = PostCodeValidator.validatePostCode(validator, "1F234", null);
+			assertTrue("Invalid Postcode", results.length == 2);		
+		}
+		
+		[Test]
+		public function countryCodePostcode():void {
+			var results:Array;
+			
+			validator.format = "CCNNNN";
+			validator.countryCode = "AA";
+			
+			results = PostCodeValidator.validatePostCode(validator, "AA1234", null);
+			assertTrue("No errors", results.length == 0);
+			
+			results = PostCodeValidator.validatePostCode(validator, "BB1234", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			
+			results = PostCodeValidator.validatePostCode(validator, "AA123", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			
+			results = PostCodeValidator.validatePostCode(validator, "AA12345", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			
+			results = PostCodeValidator.validatePostCode(validator, "1234AA", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			
+			results = PostCodeValidator.validatePostCode(validator, "A1A234", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+		}
+		
+		[Test]
+		public function spacingInPostcodes():void {
+			var results:Array;
+			
+			validator.format = "AA-NN NN";
+			
+			results = PostCodeValidator.validatePostCode(validator, "AB-12 34", null);
+			assertTrue("No errors", results.length == 0);
+			
+			results = PostCodeValidator.validatePostCode(validator, "AB 12 34", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			
+			results = PostCodeValidator.validatePostCode(validator, "AB-12-34", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			
+			results = PostCodeValidator.validatePostCode(validator, "AB-12  34", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+		}
+		
+		[Test]
+		public function alaphaNumericPostcode():void {
+			var results:Array;
+			
+			validator.format = "NNNN AA";
+			
+			results = PostCodeValidator.validatePostCode(validator, "1234 AB", null);
+			assertTrue("No errors", results.length == 0);
+			
+			results = PostCodeValidator.validatePostCode(validator, "1234-AB", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			
+			results = PostCodeValidator.validatePostCode(validator, "AB 1234", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			
+			results = PostCodeValidator.validatePostCode(validator, "12345AB", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			
+			results = PostCodeValidator.validatePostCode(validator, "1234ABC", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			
+			results = PostCodeValidator.validatePostCode(validator, "12345 AB", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+			
+			results = PostCodeValidator.validatePostCode(validator, "123 AB", null);
+			assertTrue("Invalid Postcode", results.length == 1);
+		}
+		
+	}
+}
\ No newline at end of file



Mime
View raw message