The validation code in details facet, dialog, and sections have
been modified to work more consistently.

This is required by patch #301.

--
Endi S. Dewata
From dbee685138227f6c1f663e48ac53698e595a9a89 Mon Sep 17 00:00:00 2001
From: Endi S. Dewata <edew...@redhat.com>
Date: Tue, 25 Oct 2011 18:41:45 -0500
Subject: [PATCH] Refactored validation code.

The validation code in details facet, dialog, and sections have
been modified to work more consistently.
---
 install/ui/add.js     |   18 ++--------------
 install/ui/details.js |   50 ++++++++++++++++++++++--------------------------
 install/ui/dialog.js  |   13 ++++++-----
 install/ui/entitle.js |    2 +-
 install/ui/user.js    |    2 +
 install/ui/widget.js  |    2 +-
 6 files changed, 37 insertions(+), 50 deletions(-)

diff --git a/install/ui/add.js b/install/ui/add.js
index 640604d08692077c2c5cfd67db4858506204f2a5..81d1364a2b0b633c338c0b538d6fb31902f20282 100644
--- a/install/ui/add.js
+++ b/install/ui/add.js
@@ -124,25 +124,15 @@ IPA.entity_adder_dialog = function(spec) {
 
         command.add_args(that.entity.get_primary_key_prefix());
 
+        if (!that.validate()) return;
+
         var record = {};
         that.save(record);
 
-        var fields = that.get_fields();
-        for (var i=0; i<fields.length; i++) {
-            fields[i].validate();
-        }
-
-        var valid = true;
-
         var sections = that.sections.values;
-        for (i=0; i<sections.length; i++) {
+        for (var i=0; i<sections.length; i++) {
             var section = sections[i];
 
-            if (!section.is_valid() || !valid) {
-                valid = false;
-                continue;
-            }
-
             var section_fields = section.fields.values;
             for (var j=0; j<section_fields.length; j++) {
                 var field = section_fields[j];
@@ -162,8 +152,6 @@ IPA.entity_adder_dialog = function(spec) {
             }
         }
 
-        if (!valid) return;
-
         //alert(JSON.stringify(command.to_json()));
 
         if (that.pre_execute_hook) {
diff --git a/install/ui/details.js b/install/ui/details.js
index 5c03de0a32aed46aaebd36facddceaf56a853004..3debf75d281dd93fafa4ee9a3ba06941170b6ce6 100644
--- a/install/ui/details.js
+++ b/install/ui/details.js
@@ -181,14 +181,12 @@ IPA.details_section = function(spec) {
         return false;
     };
 
-    that.is_valid = function() {
-        var fields = that.fields.values;
+    that.validate = function() {
         var valid = true;
+        var fields = that.fields.values;
         for (var i=0; i<fields.length; i++) {
             var field = fields[i];
-            if (!field.valid || !field.check_required()) {
-                valid = false;
-            }
+            valid &= field.validate() && field.validate_required();
         }
         return valid;
     };
@@ -584,6 +582,16 @@ IPA.details_facet = function(spec) {
         that.enable_update(false);
     };
 
+    that.validate = function() {
+        var valid = true;
+        var sections = that.sections.values;
+        for (var i=0; i<sections.length; i++) {
+            var section = sections[i];
+            valid &= section.validate();
+        }
+        return valid;
+    };
+
     that.update = function(on_win, on_fail) {
 
         function on_success(data, text_status, xhr) {
@@ -620,25 +628,22 @@ IPA.details_facet = function(spec) {
             on_error: on_error
         });
 
+        if (!that.validate()) {
+            var dialog = IPA.message_dialog({
+                title: IPA.messages.dialogs.validation_title,
+                message: IPA.messages.dialogs.validation_message
+            });
+            dialog.open();
+            return;
+        }
+
         var record = {};
         that.save(record);
 
-        var fields = that.get_fields();
-        for (var i=0; i<fields.length; i++) {
-            fields[i].validate();
-        }
-
-        var valid = true;
-
         var sections = that.sections.values;
-        for (i=0; i<sections.length; i++) {
+        for (var i=0; i<sections.length; i++) {
             var section = sections[i];
 
-            if (!section.is_valid() || !valid) {
-                valid = false;
-                continue;
-            }
-
             var section_fields = section.fields.values;
             for (var j=0; j<section_fields.length; j++) {
                 var field = section_fields[j];
@@ -670,15 +675,6 @@ IPA.details_facet = function(spec) {
             }
         }
 
-        if (!valid) {
-            var dialog = IPA.message_dialog({
-                title: IPA.messages.dialogs.validation_title,
-                message: IPA.messages.dialogs.validation_message
-            });
-            dialog.open();
-            return;
-        }
-
         //alert(JSON.stringify(command.to_json()));
 
         if (that.pre_execute_hook){
diff --git a/install/ui/dialog.js b/install/ui/dialog.js
index 41b35fb42120a015fcdeb56c10e80cf638456bd8..ff64b37cc549d7ea9886dc0d58a8f12267ecb839 100644
--- a/install/ui/dialog.js
+++ b/install/ui/dialog.js
@@ -66,7 +66,6 @@ IPA.dialog = function(spec) {
     that.height = spec.height;
 
     that.buttons = $.ordered_map();
-
     that.sections = $.ordered_map();
 
     var init = function() {
@@ -129,12 +128,14 @@ IPA.dialog = function(spec) {
         return that;
     };
 
-    that.is_valid = function() {
-        for (var i=0; i<that.sections.length; i++) {
-            var section = that.sections.values[i];
-            if (!section.is_valid()) return false;
+    that.validate = function() {
+        var valid = true;
+        var sections = that.sections.values;
+        for (var i=0; i<sections.length; i++) {
+            var section = sections[i];
+            valid &= section.validate();
         }
-        return true;
+        return valid;
     };
 
     that.add_section = function(section) {
diff --git a/install/ui/entitle.js b/install/ui/entitle.js
index a44318d0429f3d4c1a36b301fa1c9b15ddb97050..dcaf5638fae68c3752a5eb01d23ce8d1607d19c9 100644
--- a/install/ui/entitle.js
+++ b/install/ui/entitle.js
@@ -642,7 +642,7 @@ IPA.entitle.consume_dialog = function(spec) {
         label: IPA.messages.objects.entitle.consume,
         click: function() {
 
-            if (!that.is_valid()) {
+            if (!that.validate()) {
                 return;
             }
 
diff --git a/install/ui/user.js b/install/ui/user.js
index bfda51d84b7e34bed5f429fcdd83e0b1ea6b4e43..3ff56cd9d2bc3cfe31c1fa88674060bd4d6a9112 100644
--- a/install/ui/user.js
+++ b/install/ui/user.js
@@ -311,6 +311,8 @@ IPA.user_password_widget = function(spec) {
 
     that.create = function(container) {
 
+        that.widget_create(container);
+
         $('<a/>', {
             href: 'jslink',
             title: 'userpassword',
diff --git a/install/ui/widget.js b/install/ui/widget.js
index feaf7b2099e46624121b410c26f28f03179c2794..85a48c1de6b40863e20ae9f9fc69764d98580734 100644
--- a/install/ui/widget.js
+++ b/install/ui/widget.js
@@ -160,7 +160,7 @@ IPA.widget = function(spec) {
         }
     };
 
-    that.check_required = function() {
+    that.validate_required = function() {
         var values = that.save();
         if (!values || !values.length || values[0] === '') {
             if (that.is_required()) {
-- 
1.7.5.1

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

Reply via email to