← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 1416: Add method beforeValidateHandler for addUserForm.vm, will auto select all items in selectedList b...

 

------------------------------------------------------------
revno: 1416
committer: Viet <Viet@Viet-Laptop>
branch nick: trunk
timestamp: Thu 2010-02-11 18:25:54 +0530
message:
  Add method beforeValidateHandler for addUserForm.vm, will auto select all items in selectedList before validating.
modified:
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/util/validate/additional-methods.js
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/util/validate/jquery.validate.js
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-user/src/main/webapp/dhis-web-maintenance-user/addUserForm.vm


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription.
=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/util/validate/additional-methods.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/util/validate/additional-methods.js	2010-01-31 22:36:37 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/util/validate/additional-methods.js	2010-02-11 12:55:54 +0000
@@ -1,14 +1,23 @@
-jQuery.validator.addMethod("maxWords", function(value, element, params) { 
-    return this.optional(element) || value.match(/\b\w+\b/g).length < params; 
-}, jQuery.validator.format("Please enter {0} words or less.")); 
- 
-jQuery.validator.addMethod("minWords", function(value, element, params) { 
-    return this.optional(element) || value.match(/\b\w+\b/g).length >= params; 
-}, jQuery.validator.format("Please enter at least {0} words.")); 
- 
-jQuery.validator.addMethod("rangeWords", function(value, element, params) { 
-    return this.optional(element) || value.match(/\b\w+\b/g).length >= params[0] && value.match(/bw+b/g).length < params[1]; 
-}, jQuery.validator.format("Please enter between {0} and {1} words."));
+(function() {
+	
+	function stripHtml(value) {
+		// remove html tags and space chars
+		return value.replace(/<.[^<>]*?>/g, ' ').replace(/&nbsp;|&#160;/gi, ' ')
+		// remove numbers and punctuation
+		.replace(/[0-9.(),;:!?%#$'"_+=\/-]*/g,'');
+	}
+	jQuery.validator.addMethod("maxWords", function(value, element, params) { 
+	    return this.optional(element) || stripHtml(value).match(/\b\w+\b/g).length < params; 
+	}, jQuery.validator.format("Please enter {0} words or less.")); 
+	 
+	jQuery.validator.addMethod("minWords", function(value, element, params) { 
+	    return this.optional(element) || stripHtml(value).match(/\b\w+\b/g).length >= params; 
+	}, jQuery.validator.format("Please enter at least {0} words.")); 
+	 
+	jQuery.validator.addMethod("rangeWords", function(value, element, params) { 
+	    return this.optional(element) || stripHtml(value).match(/\b\w+\b/g).length >= params[0] && value.match(/bw+b/g).length < params[1]; 
+	}, jQuery.validator.format("Please enter between {0} and {1} words."));
+})();
 
 jQuery.validator.addMethod("letterswithbasicpunc", function(value, element) {
 	return this.optional(element) || /^[a-z-.,()'\"\s]+$/i.test(value);

=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/util/validate/jquery.validate.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/util/validate/jquery.validate.js	2010-01-24 23:52:09 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/util/validate/jquery.validate.js	2010-02-11 12:55:54 +0000
@@ -1,10 +1,10 @@
 /*
- * jQuery validation plug-in 1.5.5
+ * jQuery validation plug-in 1.6
  *
  * http://bassistance.de/jquery-plugins/jquery-plugin-validation/
  * http://docs.jquery.com/Plugins/Validation
  *
- * Copyright (c) 2006 - 2008 Jörn Zaefferer
+ * Copyright (c) 2006 - 2008 Jörn Zaefferer
  *
  * $Id: jquery.validate.js 6403 2009-06-17 14:27:16Z joern.zaefferer $
  *
@@ -53,7 +53,14 @@
 				if ( validator.settings.debug )
 					// prevent form submit to be able to see console output
 					event.preventDefault();
-					
+
+				// @Modified Viet Nguyen
+				// Call handler before validating
+				if(validator.settings.beforeValidateHandler)
+				{
+					validator.settings.beforeValidateHandler.call();	
+				}
+
 				function handle() {
 					if ( validator.settings.submitHandler ) {
 						if (validator.submitButton) {
@@ -165,9 +172,9 @@
 // Custom selectors
 $.extend($.expr[":"], {
 	// http://docs.jquery.com/Plugins/Validation/blank
-	blank: function(a) {return !$.trim(a.value);},
+	blank: function(a) {return !$.trim("" + a.value);},
 	// http://docs.jquery.com/Plugins/Validation/filled
-	filled: function(a) {return !!$.trim(a.value);},
+	filled: function(a) {return !!$.trim("" + a.value);},
 	// http://docs.jquery.com/Plugins/Validation/unchecked
 	unchecked: function(a) {return !a.checked;}
 });
@@ -233,8 +240,12 @@
 			}
 		},
 		onclick: function(element) {
+			// click on selects, radiobuttons and checkboxes
 			if ( element.name in this.submitted )
 				this.element(element);
+			// or option elements, check parent select in that case
+			else if (element.parentNode.name in this.submitted)
+				this.element(element.parentNode)
 		},
 		highlight: function( element, errorClass, validClass ) {
 			$(element).addClass(errorClass).removeClass(validClass);
@@ -250,11 +261,11 @@
 	},
 
 	//----------------------------------------------------------------------------
-	// Modified by : Viet Nguyen
+	// @Modified by : Viet Nguyen
 	// All messages should be loaded from thr message_locale.js file
 	//----------------------------------------------------------------------------
 	messages: {},
-	
+
 	autoCreateRanges: false,
 	
 	prototype: {
@@ -287,7 +298,7 @@
 			}
 			$(this.currentForm)
 				.delegate("focusin focusout keyup", ":text, :password, :file, select, textarea", delegate)
-				.delegate("click", ":radio, :checkbox", delegate);
+				.delegate("click", ":radio, :checkbox, select, option", delegate);
 
 			if (this.settings.invalidHandler)
 				$(this.currentForm).bind("invalid-form.validate", this.settings.invalidHandler);
@@ -440,7 +451,7 @@
 			this.errorMap = {};
 			this.toShow = $([]);
 			this.toHide = $([]);
-			this.formSubmitted = false;
+
 			this.currentElements = $([]);
 		},
 		
@@ -488,7 +499,7 @@
 					}
 				} catch(e) {
 					this.settings.debug && window.console && console.log("exception occured when checking element " + element.id
-						 + ", check the '" + rule.method + "' method");
+						 + ", check the '" + rule.method + "' method", e);
 					throw e;
 				}
 			}
@@ -541,13 +552,18 @@
 		},
 		
 		formatAndAdd: function( element, rule ) {
-			var message = this.defaultMessage( element, rule.method );
-			if ( typeof message == "function" ) 
+			var message = this.defaultMessage( element, rule.method ),
+				theregex = /\$?\{(\d+)\}/g;
+			if ( typeof message == "function" ) {
 				message = message.call(this, rule.parameters, element);
+			} else if (theregex.test(message)) {
+				message = jQuery.format(message.replace(theregex, '{$1}'), rule.parameters);
+			}			
 			this.errorList.push({
 				message: message,
 				element: element
 			});
+			
 			this.errorMap[element.name] = message;
 			this.submitted[element.name] = message;
 		},
@@ -603,7 +619,7 @@
 			} else {
 				// create label
 				label = $("<" + this.settings.errorElement + "/>")
-					.attr({"for":  this.idOrName(element), generated: true, style:"font-style:italic; color:red"}) // Viet NGuyen added style attr 20-11-2009
+					.attr({"for":  this.idOrName(element), generated: true, style:"font-style:italic; color:red"})// Viet Nguyen added style attr
 					.addClass(this.settings.errorClass)
 					.html(message || "");
 				if ( this.settings.wrapper ) {
@@ -612,7 +628,7 @@
 					label = label.hide().show().wrap("<" + this.settings.wrapper + "/>").parent();
 				}
 				if ( !this.labelContainer.append(label).length )
-					this.settings.errorPlacement 
+					this.settings.errorPlacement
 						? this.settings.errorPlacement(label, $(element) )
 						: label.insertAfter(element);
 			}
@@ -626,7 +642,10 @@
 		},
 		
 		errorsFor: function(element) {
-			return this.errors().filter("[for='" + this.idOrName(element) + "']");
+			var name = this.idOrName(element);
+    		return this.errors().filter(function() {
+				return $(this).attr('for') == name
+			});
 		},
 		
 		idOrName: function(element) {
@@ -693,13 +712,15 @@
 			delete this.pending[element.name];
 			if ( valid && this.pendingRequest == 0 && this.formSubmitted && this.form() ) {
 				$(this.currentForm).submit();
+				this.formSubmitted = false;
 			} else if (!valid && this.pendingRequest == 0 && this.formSubmitted) {
 				$(this.currentForm).triggerHandler("invalid-form", [this]);
+				this.formSubmitted = false;
 			}
 		},
 		
 		previousValue: function(element) {
-			return $.data(element, "previousValue") || $.data(element, "previousValue", previous = {
+			return $.data(element, "previousValue") || $.data(element, "previousValue", {
 				old: null,
 				valid: true,
 				message: this.defaultMessage( element, "remote" )
@@ -709,7 +730,7 @@
 	},
 	
 	classRuleSettings: {
-		requiredField: {required: true},
+		required: {required: true},
 		email: {email: true},
 		url: {url: true},
 		date: {date: true},
@@ -853,7 +874,7 @@
 	// http://docs.jquery.com/Plugins/Validation/Validator/addMethod
 	addMethod: function(name, method, message) {
 		$.validator.methods[name] = method;
-		$.validator.messages[name] = message || $.validator.messages[name];
+		$.validator.messages[name] = message != undefined ? message : $.validator.messages[name];
 		if (method.length < 3) {
 			$.validator.addClassRules(name, $.validator.normalizeRule(name));
 		}
@@ -868,8 +889,9 @@
 				return "dependency-mismatch";
 			switch( element.nodeName.toLowerCase() ) {
 			case 'select':
-				var options = $("option:selected", element);
-				return options.length > 0 && ( element.type == "select-multiple" || ($.browser.msie && !(options[0].attributes['value'].specified) ? options[0].text : options[0].value).length > 0);
+				// could be an array for select-multiple or a string, both are fine this way
+				var val = $(element).val();
+				return val && val.length > 0;
 			case 'input':
 				if ( this.checkable(element) )
 					return this.getLength(value, element) > 0;
@@ -884,10 +906,11 @@
 				return "dependency-mismatch";
 			
 			var previous = this.previousValue(element);
-			
+
 			if (!this.settings.messages[element.name] )
 				this.settings.messages[element.name] = {};
-			this.settings.messages[element.name].remote = typeof previous.message == "function" ? previous.message(value) : previous.message;
+			previous.originalMessage = this.settings.messages[element.name].remote;
+			this.settings.messages[element.name].remote = previous.message;
 			
 			param = typeof param == "string" && {url:param} || param; 
 			
@@ -904,6 +927,7 @@
 					dataType: "json",
 					data: data,
 					success: function(response) {
+						validator.settings.messages[element.name].remote = previous.originalMessage;
 						var valid = response === true;
 						if ( valid ) {
 							var submitted = validator.formSubmitted;
@@ -913,7 +937,8 @@
 							validator.showErrors();
 						} else {
 							var errors = {};
-							errors[element.name] = previous.message = response || validator.defaultMessage( element, "remote" );
+							var message = (previous.message = response || validator.defaultMessage( element, "remote" ));
+							errors[element.name] = $.isFunction(message) ? message(value) : message;
 							validator.showErrors(errors);
 						}
 						previous.valid = valid;
@@ -977,16 +1002,16 @@
 	
 		// http://docs.jquery.com/Plugins/Validation/Methods/dateISO
 		dateISO: function(value, element) {
-			return this.optional(element) || /^\d{4}[-]\d{1,2}[-]\d{1,2}$/.test(value);
+			return this.optional(element) || /^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/.test(value);
 		},
 	
-	
+
 		// http://docs.jquery.com/Plugins/Validation/Methods/number
 		number: function(value, element) {
 			return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value);
 		},
 	
-		
+
 		// http://docs.jquery.com/Plugins/Validation/Methods/digits
 		digits: function(value, element) {
 			return this.optional(element) || /^\d+$/.test(value);
@@ -1006,7 +1031,7 @@
 
 			value = value.replace(/\D/g, "");
 
-			for (n = value.length - 1; n >= 0; n--) {
+			for (var n = value.length - 1; n >= 0; n--) {
 				var cDigit = value.charAt(n);
 				var nDigit = parseInt(cDigit, 10);
 				if (bEven) {
@@ -1028,9 +1053,13 @@
 		
 		// http://docs.jquery.com/Plugins/Validation/Methods/equalTo
 		equalTo: function(value, element, param) {
-			return value == $(param).val();
+			// bind to the blur event of the target in order to revalidate whenever the target field is updated
+			// TODO find a way to bind the event just once, avoiding the unbind-rebind overhead
+			var target = $(param).unbind(".validate-equalTo").bind("blur.validate-equalTo", function() {
+				$(element).valid();
+			});
+			return value == target.val();
 		}
-		
 	},
 	
 	//-------------------------------------------------
@@ -1052,7 +1081,7 @@
 // ajax mode: abort
 // usage: $.ajax({ mode: "abort"[, port: "uniqueport"]});
 // if mode:"abort" is used, the previous request on that port (port can be undefined) is aborted via XMLHttpRequest.abort() 
-(function($) {
+;(function($) {
 	var ajax = $.ajax;
 	var pendingRequests = {};
 	$.ajax = function(settings) {
@@ -1076,7 +1105,7 @@
 // handler is only called when $(event.target).is(delegate), in the scope of the jquery-object for event.target 
 
 // provides triggerEvent(type: String, target: Element) to trigger delegated events
-(function($) {
+;(function($) {
 	$.each({
 		focus: 'focusin',
 		blur: 'focusout'	

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-user/src/main/webapp/dhis-web-maintenance-user/addUserForm.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-user/src/main/webapp/dhis-web-maintenance-user/addUserForm.vm	2010-02-09 09:10:29 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-user/src/main/webapp/dhis-web-maintenance-user/addUserForm.vm	2010-02-11 12:55:54 +0000
@@ -9,6 +9,10 @@
 				jQuery("#addUserForm").validate({
 					 meta:"validate"
 					,errorElement:"td"
+					,beforeValidateHandler : function()
+											{
+												selectAllById('selectedList');
+											}
 					,submitHandler: function(form)
 									{
 										validateAddUser();