Blob Blame History Raw
From a919a3ad3463eedee55b4aa2ae680c34241412b0 Mon Sep 17 00:00:00 2001
From: Pavel Vomacka <pvomacka@redhat.com>
Date: Tue, 11 Jul 2017 10:46:36 +0200
Subject: [PATCH] WebUI: Add positive number validator

Add new validator which inherits from integer validator
and checks whether the integer is positive.

https://pagure.io/freeipa/issue/6980

Reviewed-By: Felipe Volpone <felipevolpone@gmail.com>
Reviewed-By: Felipe Barreto <fbarreto@redhat.com>
---
 install/ui/src/freeipa/field.js | 43 +++++++++++++++++++++++++++++++++++++++++
 ipaserver/plugins/internal.py   |  1 +
 2 files changed, 44 insertions(+)

diff --git a/install/ui/src/freeipa/field.js b/install/ui/src/freeipa/field.js
index 76ce2533af5388ff5e1a2cfe8e35286f4e55b378..f998b578c38d91819fea418ea50e03589a691cbf 100644
--- a/install/ui/src/freeipa/field.js
+++ b/install/ui/src/freeipa/field.js
@@ -1049,9 +1049,51 @@ field.validator = IPA.validator = function(spec) {
          return that.true_result();
      };
 
+     that.integer_validate = that.validate;
+
      return that;
  };
 
+
+/**
+ * Javascript positive integer validator
+ *
+ * It allows to insert only positive integer.
+ *
+ * @class
+ * @alternateClassName IPA.positive_integer_validator
+ * @extends IPA.validator
+ */
+ field.positive_integer_validator = IPA.positive_integer_validator = function(spec) {
+
+    var that = IPA.integer_validator(spec);
+
+    /**
+    * @inheritDoc
+    */
+
+    that.validate = function(value) {
+
+        var integer_check = that.integer_validate(value);
+
+        if (!integer_check.valid) {
+            return integer_check;
+        }
+
+        var num = parseInt(value);
+
+        if (num <= 0) {
+            return that.false_result(
+                text.get('@i18n:widget.validation.positive_number'));
+        }
+
+        return that.true_result();
+    };
+
+    return that;
+ };
+
+
 /**
  * Metadata validator
  *
@@ -1871,6 +1913,7 @@ field.register = function() {
     v.register('unsupported', field.unsupported_validator);
     v.register('same_password', field.same_password_validator);
     v.register('integer', field.integer_validator);
+    v.register('positive_integer', field.positive_integer_validator);
 
     l.register('adapter', field.Adapter);
     l.register('object_adapter', field.ObjectAdapter);
diff --git a/ipaserver/plugins/internal.py b/ipaserver/plugins/internal.py
index ff239db07ec1235c4174c6f9451c71195ab5a60a..c293e0b5e06677f09daa4b820ffd06a2671cd6e1 100644
--- a/ipaserver/plugins/internal.py
+++ b/ipaserver/plugins/internal.py
@@ -982,6 +982,7 @@ class i18n_messages(Command):
                 "min_value": _("Minimum value is ${value}"),
                 "net_address": _("Not a valid network address (examples: 2001:db8::/64, 192.0.2.0/24)"),
                 "parse": _("Parse error"),
+                "positive_number": _("Must be a positive number"),
                 "port": _("'${port}' is not a valid port"),
                 "required": _("Required field"),
                 "unsupported": _("Unsupported value"),
-- 
2.13.6