← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9749: wip, warning for remote (jq) validation (used to give warning for duplicate names), only uses ale...

 

------------------------------------------------------------
revno: 9749
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2013-02-06 19:33:38 +0700
message:
  wip, warning for remote (jq) validation (used to give warning for duplicate names), only uses alert() for now, but rest of logic is in place.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/commons.js
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/jQuery/jquery.validate.ext.js
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/webapp/dhis-web-maintenance-organisationunit/addOrganisationUnitForm.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-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java	2013-02-04 18:03:10 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java	2013-02-06 12:33:38 +0000
@@ -572,12 +572,6 @@
     // -------------------------------------------------------------------------
 
     @Override
-    public int hashCode()
-    {
-        return name.hashCode();
-    }
-
-    @Override
     public boolean equals( Object o )
     {
         if ( this == o )

=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/commons.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/commons.js	2012-12-07 08:10:30 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/commons.js	2013-02-06 12:33:38 +0000
@@ -1188,7 +1188,8 @@
 	var rules = kwargs["rules"];
 	var validator = jQuery( "#" + formId ).validate( {
 		meta: "validate",
-		errorElement: "span",
+        errorElement: "span",
+        warningElement: "span",
 		beforeValidateHandler: beforeValidateHandler,
 		submitHandler: submitHandler,
 		rules: rules,
@@ -1267,6 +1268,17 @@
 	});
 }
 
+function checkValueIsExistWarning( inputId, url, params )
+{
+	jQuery("#" + inputId).rules("add",{
+		remoteWarning: {
+			url:url,
+			type:'post',
+			data:params
+		}
+	});
+}
+
 function remoteValidateById( inputId, url, params )
 {
 	jQuery("#" + inputId).rules("add",{

=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/jQuery/jquery.validate.ext.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/jQuery/jquery.validate.ext.js	2012-10-30 08:12:59 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/jQuery/jquery.validate.ext.js	2013-02-06 12:33:38 +0000
@@ -1,3 +1,71 @@
+
+jQuery.validator.addMethod("remoteWarning", function(value, element, param) {
+    if ( this.optional(element) )
+        return "dependency-mismatch";
+
+    var previous = this.previousValue(element);
+    if (!this.settings.messages[element.name] )
+        this.settings.messages[element.name] = {};
+    previous.originalMessage = this.settings.messages[element.name].remote;
+    this.settings.messages[element.name].remote = previous.message;
+
+    param = typeof param == "string" && {url:param} || param;
+
+    if ( this.pending[element.name] ) {
+        return "pending";
+    }
+    if ( previous.old === value ) {
+        return previous.valid;
+    }
+
+    previous.old = value;
+    var validator = this;
+    this.startRequest(element);
+    var data = {};
+    data[element.name] = value;
+    $.ajax($.extend(true, {
+        url: param,
+        mode: "abort",
+        port: "validate" + element.name,
+        dataType: "json",
+        data: data,
+        success: function(response) {
+            validator.settings.messages[element.name].remote = previous.originalMessage;
+            var valid = response.response === 'success';
+
+            if ( valid ) {
+                console.log("is valid")
+                var submitted = validator.formSubmitted;
+                validator.prepareElement(element);
+                validator.formSubmitted = submitted;
+                validator.successList.push(element);
+                validator.showErrors();
+            } else {
+                var message = (previous.message = response.message || validator.defaultMessage( element, "remote" ));
+                alert(message);
+
+                var submitted = validator.formSubmitted;
+                validator.prepareElement(element);
+                validator.formSubmitted = submitted;
+                validator.successList.push(element);
+                validator.showErrors();
+
+                /*
+                var errors = {};
+                var message = (previous.message = response.message || validator.defaultMessage( element, "remote" ));
+
+                errors[element.name] = previous.message = $.isFunction(message) ? message(value) : message;
+                validator.showErrors(errors);
+                */
+            }
+
+            previous.valid = true;
+            validator.stopRequest(element, true);
+        }
+    }, param));
+    return "pending";
+});
+
 methods_en_GB = function() {
     jQuery.validator.addMethod("dateI", function(value, element, params) {
         var check = false;

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/webapp/dhis-web-maintenance-organisationunit/addOrganisationUnitForm.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/webapp/dhis-web-maintenance-organisationunit/addOrganisationUnitForm.vm	2013-02-04 14:57:16 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/webapp/dhis-web-maintenance-organisationunit/addOrganisationUnitForm.vm	2013-02-06 12:33:38 +0000
@@ -11,7 +11,7 @@
 			'rules' : getValidationRules("organisationUnit")
 		});
 
-        checkValueIsExist("name", "validateOrganisationUnit.action");
+        checkValueIsExistWarning("name", "validateOrganisationUnit.action");
         checkValueIsExist("code", "validateOrganisationUnit.action");
         datePickerValid('openingDate', false);