From 9868f4ec0609bcdf4684df97b8ada90b838807df Mon Sep 17 00:00:00 2001
From: mikl <>
Date: Tue, 27 May 2008 15:47:17 +0000
Subject: Added Prado.Validation.validateControl(id) on client side

---
 .../source/prado/validator/validation3.js          | 72 +++++++++++++++++++---
 1 file changed, 65 insertions(+), 7 deletions(-)

(limited to 'framework/Web')

diff --git a/framework/Web/Javascripts/source/prado/validator/validation3.js b/framework/Web/Javascripts/source/prado/validator/validation3.js
index 5f08b615..c9dba7a1 100644
--- a/framework/Web/Javascripts/source/prado/validator/validation3.js
+++ b/framework/Web/Javascripts/source/prado/validator/validation3.js
@@ -71,6 +71,8 @@ Prado.Validation =  Class.create();
  * where <tt>formID</tt> is the HTML form ID, and the optional
  * <tt>groupID</tt> if present will only validate the validators
  * in a particular group.</p>
+ * <p>Use <code>{@link Prado.Validation.validateControl}(controlClientID)</code>
+ * to trigger validation for a single control.</p>
  * 
  * @object {static} Prado.Validation
  */
@@ -106,6 +108,24 @@ Object.extend(Prado.Validation,
 		}
 	},
 
+	/**
+	 * Validate all validators of a specific control.
+	 * @function {boolean} ?
+	 * @param {string} id - ID of DOM element to validate 
+	 * @return true if all validators are valid or no validators present, false otherwise.
+	 */
+    validateControl : function(id) 
+    {
+        var formId=this.getForm();
+
+		if (this.managers[formId])
+        {
+            return this.managers[formId].validateControl(id);
+        } else {
+			throw new Error("A validation manager needs to be created first.");
+        }
+    },
+
 	/**
 	 * Return first registered form
 	 * @function {string} ?
@@ -208,6 +228,12 @@ Object.extend(Prado.Validation,
 Prado.ValidationManager = Class.create();
 Prado.ValidationManager.prototype =
 {
+	/**
+	 * Hash of registered validators by control's clientID
+	 * @var controls
+	 */
+    controls: {},
+
 	/**
 	 * Initialize ValidationManager.
 	 * @constructor {protected} ?
@@ -283,6 +309,17 @@ Prado.ValidationManager.prototype =
 		return valid;
 	},
 
+	/**
+	 * Perform validation for all validators of a single control.
+	 * @function {boolean} ?
+	 * @param {string} id - ID of DOM element to validate 
+	 * @return true if all validators are valid or no validators present, false otherwise.
+	 */
+    validateControl : function (id) 
+    {
+        return this.controls[id] ? this.controls[id].invoke('validate',null).all() : true;
+    },
+
 	/**
 	 * Focus on the first validator that is invalid and options.FocusOnError is true.
 	 * @function ?
@@ -371,14 +408,17 @@ Prado.ValidationManager.prototype =
 	 */
 	addValidator : function(validator)
 	{
-		// Erase any existing validator with same options
-		this.validators = this.validators.reject(function(v)
-		{
-			return (v.options.ID==validator.options.ID);
-		});
+		// Remove previously registered validator with same ID
+        // to prevent stale validators created by AJAX updates
+        this.removeValidator(validator);
+
 		this.validators.push(validator);
 		if(validator.group && !this.groups.include(validator.group))
 			this.groups.push(validator.group);
+
+        if (typeof this.controls[validator.control.id] === 'undefined')
+            this.controls[validator.control.id] = Array();
+        this.controls[validator.control.id].push(validator);
 	},
 
 	/**
@@ -391,6 +431,24 @@ Prado.ValidationManager.prototype =
 		this.summaries.push(summary);
 	},
 
+	/**
+	 * Remove a validator from this manager
+	 * @function ?
+	 * @param {TBaseValidator} validator - Validator object
+	 */
+    removeValidator : function(validator)
+    {
+		this.validators = this.validators.reject(function(v)
+		{
+			return (v.options.ID==validator.options.ID);
+		});
+        if (this.controls[validator.control.id])
+            this.controls[validator.control.id].reject( function(v)
+            {
+                return (v.options.ID==validator.options.ID)
+            });
+    },
+
 	/**
 	 * Gets validators with errors.
 	 * If group is set, only validators in that group are returned. 
@@ -473,7 +531,7 @@ Prado.WebUI.TValidationSummary.prototype =
 	initialize : function(options)
 	{
 		/**
-		 * ValidationManager options
+		 * Validator options
 		 * @var {object} options 
 		 */
 		this.options = options;
@@ -705,7 +763,7 @@ Prado.WebUI.TBaseValidator.prototype =
 	 * Initialize TBaseValidator.
 	 * @constructor {protected} ?
 	 * @param {object} options - Options for initialization.
-	 * @... {string} ID - ID of validation summary element.
+	 * @... {string} ID - ID of validator
 	 * @... {string} FormID - ID of form of this manager.
 	 * @... {string} ControlToValidate - ID of control to validate.
 	 * @... {optional string} InitialValue - Initial value of control to validate.
-- 
cgit v1.2.3