← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20071: add dhis2.validation method for validating value types, assumes global i18n variables

 

------------------------------------------------------------
revno: 20071
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-09-10 15:53:19 +0700
message:
  add dhis2.validation method for validating value types, assumes global i18n variables
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/resources/i18n_global.properties
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.validation.js
  dhis-2/dhis-web/dhis-web-dataentry/src/main/resources/org/hisp/dhis/de/i18n_module.properties


--
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-services/dhis-service-core/src/main/resources/i18n_global.properties'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/i18n_global.properties	2015-09-09 09:01:21 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/i18n_global.properties	2015-09-10 08:53:19 +0000
@@ -564,6 +564,16 @@
 yes_only = Yes Only
 custom=Custom
 
+#-- Validation messages -------------------------------------------------------#
+
+value_must_integer=Value must be an integer
+value_must_number=Value must be a number
+value_must_positive_integer=Value must be a positive integer
+value_must_negative_integer=Value must be a negative integer
+value_must_zero_or_positive_integer=Value must be zero or a positive integer
+value_must_unit_interval=Value must be a unit interval (between 0 and 1)
+value_must_percentage=Value must be a percentage (between 0 and 100)
+
 #-- User account --------------------------------------------------------------#
 
 profile=Profile

=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.validation.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.validation.js	2015-05-22 10:21:44 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.validation.js	2015-09-10 08:53:19 +0000
@@ -46,9 +46,9 @@
 dhis2.validation.isInt = function(value) {
   var regex = /^(0|-?[1-9]\d*)$/;
   var patternTest = regex.test(value);
-  var rangeTest = value && 
-  	parseInt(value) < dhis2.validation.INT_MAX_VALUE &&
-  	parseInt(value) > ( dhis2.validation.INT_MAX_VALUE * -1 );
+  var rangeTest = value &&
+    parseInt(value) < dhis2.validation.INT_MAX_VALUE &&
+    parseInt(value) > ( dhis2.validation.INT_MAX_VALUE * -1 );
   return patternTest && rangeTest;
 };
 
@@ -100,7 +100,7 @@
 };
 
 /**
- * Allow only integers inclusive between 0 and 100. 
+ * Allow only integers inclusive between 0 and 100.
  */
 dhis2.validation.isPercentage = function(value) {
   return dhis2.validation.isInt(value) && parseInt(value) >= 0 && parseInt(value) <= 100;
@@ -120,3 +120,87 @@
 
   return f >= 0 && f <= 1;
 };
+
+/**
+ * Validate value type. To have proper message displayed, requires the following vars to be available:
+ * i18n_value_must_integer
+ * i18n_value_must_number
+ * i18n_value_must_positive_integer
+ * i18n_value_must_zero_or_positive_integer
+ * i18n_value_must_negative_integer
+ * i18n_value_must_unit_interval
+ * i18n_value_must_percentage
+ *
+ * @param value Value to check against
+ * @param valueType Value type (from data element, option set, etc)
+ */
+dhis2.validation.isValidValueType = function(value, valueType) {
+  switch( valueType ) {
+    case 'TEXT':
+    case 'LONG_TEXT':
+    case 'USERNAME':
+    case 'DATE':
+    case 'DATETIME':
+    {
+      break;
+    }
+    case 'INTEGER':
+    {
+      if( !dhis2.validation.isInt(value) ) {
+        setHeaderDelayMessage(i18n_value_must_integer);
+        return false;
+      }
+      break;
+    }
+    case 'INTEGER_POSITIVE':
+    {
+      if( !dhis2.validation.isPositiveInt(value) ) {
+        setHeaderDelayMessage(i18n_value_must_positive_integer);
+        return false;
+      }
+      break;
+    }
+    case 'INTEGER_NEGATIVE':
+    {
+      if( !dhis2.validation.isNegativeInt(value) ) {
+        setHeaderDelayMessage(i18n_value_must_negative_integer);
+        return false;
+      }
+      break;
+    }
+    case 'INTEGER_ZERO_OR_POSITIVE':
+    {
+      if( !dhis2.validation.isZeroOrPositiveInt(value) ) {
+        setHeaderDelayMessage(i18n_value_must_zero_or_positive_integer);
+        return false;
+      }
+      break;
+    }
+    case 'NUMBER':
+    {
+      if( !dhis2.validation.isNumber(value) ) {
+        setHeaderDelayMessage(i18n_value_must_number);
+        return false;
+      }
+      break;
+    }
+    case 'UNIT_INTERVAL':
+    {
+      if( !dhis2.validation.isUnitInterval(value) ) {
+        setHeaderDelayMessage(i18n_value_must_unit_interval);
+        return false;
+      }
+      break;
+    }
+    case 'PERCENTAGE':
+    {
+      if( !dhis2.validation.isPercentage(value) ) {
+        setHeaderDelayMessage(i18n_value_must_percentage);
+        return false;
+      }
+      break;
+    }
+  }
+
+  return true;
+};

=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/resources/org/hisp/dhis/de/i18n_module.properties'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/resources/org/hisp/dhis/de/i18n_module.properties	2015-03-29 20:32:18 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/resources/org/hisp/dhis/de/i18n_module.properties	2015-09-10 08:53:19 +0000
@@ -25,7 +25,6 @@
 later_periods=Later periods
 value_of_data_element_less=The value of the following data element is less than the minimum accepted value
 value_of_data_element_greater=The value of the following data element is greater than the maximum accepted value
-value_must_integer=Value must be an integer
 saving_value_failed_status_code=Saving value failed with status code
 saving_value_failed_dataset_is_locked=Data set is locked, please contact admin
 saving_comment_failed_status_code=Saving comment failed with status code
@@ -65,12 +64,6 @@
 undo_register_complete_dataset_success=Undo register complete data set success
 datavalue_history=Data value history
 enter_digits=Enter digit only.
-value_must_number=Value must be a number
-value_must_positive_integer=Value must be a positive integer
-value_must_negative_integer=Value must be a negative integer
-value_must_zero_or_positive_integer=Value must be zero or a positive integer
-value_must_unit_interval=Value must be a unit interval (between 0 and 1)
-value_must_percentage=Value must be a percentage (between 0 and 100)
 value_is_too_long=Value is too long
 field_unallowed_save_zero=This element does not permit 0 values. The original value will not be affected.
 datavalue_history=Data value history