The widget base class has been modified to validate integer value
if the type is specified in the metadata. This is used to validate
entitlement quantity.

--
Endi S. Dewata
From 0ff8501614b78f5ccff3908f459fdbc5e58e8241 Mon Sep 17 00:00:00 2001
From: Endi S. Dewata <edew...@redhat.com>
Date: Tue, 26 Apr 2011 16:21:25 -0500
Subject: [PATCH] Entitlement quantity validation.

The widget base class has been modified to validate integer value
if the type is specified in the metadata. This is used to validate
entitlement quantity.

---
 install/ui/dialog.js  |   10 +++++-
 install/ui/dns.js     |    2 +-
 install/ui/entitle.js |   12 +++++--
 install/ui/host.js    |    2 +-
 install/ui/ipa.js     |   23 ++++++++++++-
 install/ui/service.js |    4 +-
 install/ui/widget.js  |   86 ++++++++++++++++++++++++++++++++----------------
 7 files changed, 101 insertions(+), 38 deletions(-)

diff --git a/install/ui/dialog.js b/install/ui/dialog.js
index f60db5cabf9341c67c80afe2dd3b883398aced12..2c9fdb0e24515001ed84d20b97937823ed2f9098 100644
--- a/install/ui/dialog.js
+++ b/install/ui/dialog.js
@@ -74,11 +74,19 @@ IPA.dialog = function(spec) {
         that.fields_by_name[field.name] = field;
     };
 
-    that.field = function(field){
+    that.field = function(field) {
         that.add_field(field);
         return that;
     };
 
+    that.is_valid = function() {
+        for (var i=0; i<that.fields.length; i++) {
+            var field = that.fields[i];
+            if (!field.valid) return false;
+        }
+        return true;
+    };
+
     that.text = function(name){
         that.field(IPA.text_widget({
             name: name,
diff --git a/install/ui/dns.js b/install/ui/dns.js
index f52a06429a23c7352cdb78bff744ca7843c072b0..5cb49e55f8894e752834c2502c4314b4b6821835 100644
--- a/install/ui/dns.js
+++ b/install/ui/dns.js
@@ -69,7 +69,7 @@ IPA.entity_factories.dnszone = function() {
 };
 
 IPA.force_dnszone_add_checkbox_widget = function (spec){
-    var param_info = IPA.get_method_param('dnszone_add', 'force');
+    var param_info = IPA.get_method_option('dnszone_add', 'force');
     spec.name = 'force';
     spec.label = param_info.label;
     spec.tooltip = param_info.doc;
diff --git a/install/ui/entitle.js b/install/ui/entitle.js
index 1b434d3fbbfa9040d21c87fa19d7c9e20ec6999d..193468c2aa98e21897c51bb8a3a5b0c620ef2b60 100644
--- a/install/ui/entitle.js
+++ b/install/ui/entitle.js
@@ -86,7 +86,7 @@ IPA.entity_factories.entitle = function() {
                 },
                 {
                     name: 'password',
-                    label: IPA.get_method_param('entitle_register', 'password').label,
+                    label: IPA.get_method_option('entitle_register', 'password').label,
                     type: 'password',
                     undo: false
                 }
@@ -114,7 +114,8 @@ IPA.entity_factories.entitle = function() {
                 {
                     name: 'quantity',
                     label: 'Quantity',
-                    undo: false
+                    undo: false,
+                    metadata: IPA.get_method_arg('entitle_consume', 'quantity')
                 }
             ]
         }).
@@ -261,7 +262,6 @@ IPA.entitle.search_facet = function(spec) {
 
     that.setup = function(container) {
 
-
         that.search_facet_setup(container);
 
         var buttons = that.entity_header.buttons;
@@ -408,6 +408,7 @@ IPA.entitle.certificate_column = function(spec) {
     var that = IPA.column(spec);
 
     that.setup = function(container, record) {
+
         container.empty();
 
         var certificate = record[that.name];
@@ -527,6 +528,11 @@ IPA.entitle.consume_dialog = function(spec) {
     var that = IPA.dialog(spec);
 
     that.add_button('Consume', function() {
+
+        if (!that.is_valid()) {
+            return;
+        }
+
         var record = {};
         that.save(record);
 
diff --git a/install/ui/host.js b/install/ui/host.js
index 44479de6b843020421aaa57f4bd871c86212b94f..bd694ee611e47843791087b576ea294180932b25 100644
--- a/install/ui/host.js
+++ b/install/ui/host.js
@@ -122,7 +122,7 @@ IPA.utc_date_column_format = function(value){
 
 
 IPA.force_host_add_checkbox_widget = function (spec){
-    var param_info = IPA.get_method_param('host_add', 'force');
+    var param_info = IPA.get_method_option('host_add', 'force');
     spec.name = 'force';
     spec.label = param_info.label;
     spec.tooltip = param_info.doc;
diff --git a/install/ui/ipa.js b/install/ui/ipa.js
index 60b27633addc2251e319706e0273f4e5bf265cac..f551fc64bd14afbdbfe64beeb633dc2a9e6d0df9 100644
--- a/install/ui/ipa.js
+++ b/install/ui/ipa.js
@@ -539,7 +539,28 @@ IPA.get_entity_param = function(entity_name, name) {
     return null;
 };
 
-IPA.get_method_param = function(method_name, name) {
+IPA.get_method_arg = function(method_name, name) {
+
+    var metadata = IPA.metadata.methods[method_name];
+    if (!metadata) {
+        return null;
+    }
+
+    var args = metadata.takes_args;
+    if (!args) {
+        return null;
+    }
+
+    for (var i=0; i<args.length; i++) {
+        if (args[i].name === name) {
+            return args[i];
+        }
+    }
+
+    return null;
+};
+
+IPA.get_method_option = function(method_name, name) {
 
     var metadata = IPA.metadata.methods[method_name];
     if (!metadata) {
diff --git a/install/ui/service.js b/install/ui/service.js
index daf4e6212943a4d158230b0dd96245a8af0b19f4..f76af900cd0a03cd1023facd27e0d58f4b865604 100644
--- a/install/ui/service.js
+++ b/install/ui/service.js
@@ -127,8 +127,8 @@ IPA.service_add_dialog = function(spec) {
         field(
         IPA.checkbox_widget({
             name: 'force',
-            label: IPA.get_method_param('service_add', 'force').label,
-            tooltip: IPA.get_method_param('service_add', 'force').doc,
+            label: IPA.get_method_option('service_add', 'force').label,
+            tooltip: IPA.get_method_option('service_add', 'force').doc,
             undo: false
         }));
 
diff --git a/install/ui/widget.js b/install/ui/widget.js
index ba02a84407f3436217275fdccc56bc4ae95e1158..a376c3ac68bf6979217b97a0197f3d9d65662b99 100644
--- a/install/ui/widget.js
+++ b/install/ui/widget.js
@@ -57,10 +57,12 @@ IPA.widget = function(spec) {
     that.load = spec.load || load;
     that.save = spec.save || save;
     that.update = spec.update || update;
-    that.validate_input = spec.validate_input || validate_input;
-    that.valid = true;
+
     that.param_info = spec.param_info;
+    that.metadata = spec.metadata;
+
     that.values = [];
+    that.valid = true;
 
     that.__defineGetter__("entity_name", function(){
         return that._entity_name;
@@ -73,29 +75,58 @@ IPA.widget = function(spec) {
     /*returns true and clears the error message if the field value  passes
       the validation pattern.  If the field value does not pass validation,
       displays the error message and returns false. */
-    function validate_input(text) {
-        if (!(that.param_info && that.param_info.pattern)) {
-            that.valid = true;
+    that.validate = function() {
+
+        that.hide_error();
+
+        that.valid = true;
+
+        var values = that.save();
+        if (!values || !values.length) {
             return;
         }
-        var error_link = that.get_error_link();
-        if (!error_link) {
-            that.valid = true;
+
+        var value = values[0];
+        if (!value) {
             return;
         }
-        var regex = new RegExp( that.param_info.pattern );
-        //If the field is empty, don't validate
-        if ( !text || text.match(regex) ) {
-            error_link.css('display', 'none');
-            that.valid = true;
-        } else {
-            error_link.css('display', 'block');
-            if (that.param_info.pattern_errmsg) {
-                error_link.html(that.param_info.pattern_errmsg);
+
+        if (that.metadata) {
+            if (that.metadata.type == 'int') {
+                if (!value.match(/^-?\d+$/)) {
+                    that.valid = false;
+                    // TODO: I18n
+                    that.show_error('must be an integer');
+                    return;
+                }
+
+                if (that.metadata.minvalue && value < that.metadata.minvalue) {
+                    that.valid = false;
+                    // TODO: I18n
+                    that.show_error('minimum value is '+that.metadata.minvalue);
+                    return;
+                }
+
+                if (that.metadata.maxvalue && value > that.metadata.maxvalue) {
+                    that.valid = false;
+                    // TODO: I18n
+                    that.show_error('maximum value is '+that.metadata.maxvalue);
+                    return;
+                }
             }
-            that.valid = false;
         }
-    }
+
+        if (that.param_info) {
+            if (that.param_info.pattern) {
+                var regex = new RegExp(that.param_info.pattern);
+                if (!value.match(regex)) {
+                    that.valid = false;
+                    that.show_error(that.param_info.pattern_errmsg);
+                    return;
+                }
+            }
+        }
+    };
 
     function init() {
         if (that.entity_name) {
@@ -247,12 +278,13 @@ IPA.widget = function(spec) {
         return $('span[name="error_link"]', that.container);
     };
 
-    that.show_error_link = function() {
+    that.show_error = function(message) {
         var error_link = that.get_error_link();
-        error_link.css('display', 'inline');
+        error_link.html(message);
+        error_link.css('display', 'block');
     };
 
-    that.hide_error_link = function() {
+    that.hide_error = function() {
         var error_link = that.get_error_link();
         error_link.css('display', 'none');
     };
@@ -323,8 +355,7 @@ IPA.text_widget = function(spec) {
             if (that.undo) {
                 that.show_undo();
             }
-            var value = $(this).val();
-            that.validate_input(value);
+            that.validate();
         });
 
         var undo = that.get_undo();
@@ -539,8 +570,7 @@ IPA.multivalued_text_widget = function(spec) {
                         remove_link.css('display', 'inline');
                     }
                 }
-                var value = $(this).val();
-                that.validate_input(value);
+                that.validate();
             });
 
             remove_link.click(function() {
@@ -965,9 +995,7 @@ IPA.textarea_widget = function (spec) {
         var input = $('textarea[name="'+that.name+'"]', that.container);
         input.keyup(function() {
             that.show_undo();
-
-            var value = $(this).val();
-            that.validate_input(value);
+            that.validate();
 
         });
 
-- 
1.7.4

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to