3f51ca
From a919a3ad3463eedee55b4aa2ae680c34241412b0 Mon Sep 17 00:00:00 2001
3f51ca
From: Pavel Vomacka <pvomacka@redhat.com>
3f51ca
Date: Tue, 11 Jul 2017 10:46:36 +0200
3f51ca
Subject: [PATCH] WebUI: Add positive number validator
3f51ca
3f51ca
Add new validator which inherits from integer validator
3f51ca
and checks whether the integer is positive.
3f51ca
3f51ca
https://pagure.io/freeipa/issue/6980
3f51ca
3f51ca
Reviewed-By: Felipe Volpone <felipevolpone@gmail.com>
3f51ca
Reviewed-By: Felipe Barreto <fbarreto@redhat.com>
3f51ca
---
3f51ca
 install/ui/src/freeipa/field.js | 43 +++++++++++++++++++++++++++++++++++++++++
3f51ca
 ipaserver/plugins/internal.py   |  1 +
3f51ca
 2 files changed, 44 insertions(+)
3f51ca
3f51ca
diff --git a/install/ui/src/freeipa/field.js b/install/ui/src/freeipa/field.js
3f51ca
index 76ce2533af5388ff5e1a2cfe8e35286f4e55b378..f998b578c38d91819fea418ea50e03589a691cbf 100644
3f51ca
--- a/install/ui/src/freeipa/field.js
3f51ca
+++ b/install/ui/src/freeipa/field.js
3f51ca
@@ -1049,9 +1049,51 @@ field.validator = IPA.validator = function(spec) {
3f51ca
          return that.true_result();
3f51ca
      };
3f51ca
 
3f51ca
+     that.integer_validate = that.validate;
3f51ca
+
3f51ca
      return that;
3f51ca
  };
3f51ca
 
3f51ca
+
3f51ca
+/**
3f51ca
+ * Javascript positive integer validator
3f51ca
+ *
3f51ca
+ * It allows to insert only positive integer.
3f51ca
+ *
3f51ca
+ * @class
3f51ca
+ * @alternateClassName IPA.positive_integer_validator
3f51ca
+ * @extends IPA.validator
3f51ca
+ */
3f51ca
+ field.positive_integer_validator = IPA.positive_integer_validator = function(spec) {
3f51ca
+
3f51ca
+    var that = IPA.integer_validator(spec);
3f51ca
+
3f51ca
+    /**
3f51ca
+    * @inheritDoc
3f51ca
+    */
3f51ca
+
3f51ca
+    that.validate = function(value) {
3f51ca
+
3f51ca
+        var integer_check = that.integer_validate(value);
3f51ca
+
3f51ca
+        if (!integer_check.valid) {
3f51ca
+            return integer_check;
3f51ca
+        }
3f51ca
+
3f51ca
+        var num = parseInt(value);
3f51ca
+
3f51ca
+        if (num <= 0) {
3f51ca
+            return that.false_result(
3f51ca
+                text.get('@i18n:widget.validation.positive_number'));
3f51ca
+        }
3f51ca
+
3f51ca
+        return that.true_result();
3f51ca
+    };
3f51ca
+
3f51ca
+    return that;
3f51ca
+ };
3f51ca
+
3f51ca
+
3f51ca
 /**
3f51ca
  * Metadata validator
3f51ca
  *
3f51ca
@@ -1871,6 +1913,7 @@ field.register = function() {
3f51ca
     v.register('unsupported', field.unsupported_validator);
3f51ca
     v.register('same_password', field.same_password_validator);
3f51ca
     v.register('integer', field.integer_validator);
3f51ca
+    v.register('positive_integer', field.positive_integer_validator);
3f51ca
 
3f51ca
     l.register('adapter', field.Adapter);
3f51ca
     l.register('object_adapter', field.ObjectAdapter);
3f51ca
diff --git a/ipaserver/plugins/internal.py b/ipaserver/plugins/internal.py
3f51ca
index ff239db07ec1235c4174c6f9451c71195ab5a60a..c293e0b5e06677f09daa4b820ffd06a2671cd6e1 100644
3f51ca
--- a/ipaserver/plugins/internal.py
3f51ca
+++ b/ipaserver/plugins/internal.py
3f51ca
@@ -982,6 +982,7 @@ class i18n_messages(Command):
3f51ca
                 "min_value": _("Minimum value is ${value}"),
3f51ca
                 "net_address": _("Not a valid network address (examples: 2001:db8::/64, 192.0.2.0/24)"),
3f51ca
                 "parse": _("Parse error"),
3f51ca
+                "positive_number": _("Must be a positive number"),
3f51ca
                 "port": _("'${port}' is not a valid port"),
3f51ca
                 "required": _("Required field"),
3f51ca
                 "unsupported": _("Unsupported value"),
3f51ca
-- 
3f51ca
2.13.6
3f51ca