Patron reg field validation (WIP)
authorBill Erickson <berickxx@gmail.com>
Mon, 29 Feb 2016 14:13:10 +0000 (09:13 -0500)
committerBill Erickson <berickxx@gmail.com>
Mon, 29 Feb 2016 14:13:10 +0000 (09:13 -0500)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/templates/staff/circ/patron/t_edit.tt2
Open-ILS/web/js/ui/default/staff/circ/patron/regctl.js

index be71f43..d9dda61 100644 (file)
@@ -158,6 +158,7 @@ within the "form" by name for validation.
     <input type="text" 
       name='passwd'
       ng-class="{'patron-reg-invalid-field' : field_is_invalid('passwd')}"
+      ng-pattern="patterns.au.passwd"
       ng-required="true"
       ng-change="field_modified()" 
       ng-blur="handle_field_changed(patron, 'passwd')"
index a0816f2..9bc73a1 100644 (file)
@@ -1016,6 +1016,50 @@ function PatronRegCtrl($scope, $routeParams,
         }
     }
 
+    // Loads field regex's into the scope for field validation.
+    function inject_field_patterns() {
+        $scope.patterns = {au : {}, aua : {}, ac : {}};
+
+        if ($scope.org_settings['opac.username_regex']) {
+            $scope.patterns.au.usrname = 
+                new RegExp($scope.org_settings['opac.username_regex']);
+        }
+
+        if ($scope.org_settings['opac.barcode_regex']) {
+            $scope.patterns.ac.barcode = 
+                new RegExp($scope.org_settings['opac.barcode_regex']);
+        }
+
+        if ($scope.org_settings['global.password_regex']) {
+            $scope.patterns.au.passwd = 
+                new RegExp($scope.org_settings['global.password_regex']);
+        }
+
+        console.log($scope.patterns.au.passwd);
+
+        if ($scope.org_settings['ui.patron.edit.phone.regex']) {
+            // apply generic phone regex first, replace below as needed.
+
+            $scope.patterns.au.day_phone = 
+                new RegExp($scope.org_settings['ui.patron.edit.phone.regex']);
+            $scope.patterns.au.evening_phone = 
+                new RegExp($scope.org_settings['ui.patron.edit.phone.regex']);
+            $scope.patterns.au.other_phone = 
+                new RegExp($scope.org_settings['ui.patron.edit.phone.regex']);
+        }
+
+        // the remaining patterns fit a well-known key name pattern
+
+        angular.forEach($scope.org_settings, function(val, key) {
+            if (!val) return;
+            var parts = key.match(/ui.patron.edit\.(\w+)\.(\w+)\.regex/);
+            if (!parts) return;
+            var cls = parts[1];
+            var name = parts[2];
+            $scope.patterns[cls][name] = new RegExp(val);
+        });
+    }
+
     $q.all([
 
         $scope.initTab ? // initTab comes from patron app
@@ -1062,6 +1106,8 @@ function PatronRegCtrl($scope, $routeParams,
             set_new_patron_defaults(prs);
 
         $scope.page_data_loaded = true;
+
+        inject_field_patterns();
     });
 
     // update the currently displayed field documentation
@@ -1420,18 +1466,11 @@ function PatronRegCtrl($scope, $routeParams,
     // The alternative is ng-change, but it's called with each character
     // typed, which would be overkill for many of the actions called here.
     $scope.handle_field_changed = function(obj, field_name) {
-
         var cls = obj.classname; // set by egIdl
         var value = obj[field_name];
 
         console.log('changing field ' + field_name + ' to ' + value);
 
-        console.log($scope.reg_form.usrname.$error);
-
-        if (field_name == 'usrname') {
-            console.log('invalid : ' + $scope.field_is_invalid('usrname'));
-        }
-
         switch (field_name) {
             case 'day_phone' : 
                 if ($scope.patron.day_phone && 
@@ -1489,13 +1528,16 @@ function PatronRegCtrl($scope, $routeParams,
         $scope.field_modified();
     }
 
+    // Returns true if a required field has no value or a field's
+    // value does not match its configured regex pattern.
     // Tests angular's form field validation toggles.
     $scope.field_is_invalid = function(field_name) {
-        console.log('err: ' + field_name + ' : ' 
-            + $scope.reg_form[field_name].$error.required);
-        return 
-            $scope.reg_form[field_name].$error.required ||
-            !$scope.reg_form[field_name].$error.valid;
+        return (
+            $scope.reg_form[field_name].$error.required === true 
+            ||
+            // valid will be undefined if no pattern is set.
+            $scope.reg_form[field_name].$error.valid === false
+        );
     }