CRM-15856 - crmUi - Add generic, field-independent validator (crmUiValidate).
authorTim Otten <totten@civicrm.org>
Sat, 24 Jan 2015 06:27:05 +0000 (22:27 -0800)
committerTim Otten <totten@civicrm.org>
Sat, 24 Jan 2015 08:02:06 +0000 (00:02 -0800)
js/angular-crm-ui.js

index 1d18f714c29a156922caa85de099d8d03e671caf..44b19bdb280e104578148adaf1efc952ad5a764e 100644 (file)
       };
     })
 
+    // example: <span ng-model="placeholder" crm-ui-validate="foo && bar || whiz" />
+    // example: <span ng-model="placeholder" crm-ui-validate="foo && bar || whiz" crm-ui-validate-name="myError" />
+    // Generic, field-independent validator.
+    .directive('crmUiValidate', function() {
+      return {
+        restrict: 'EA',
+        require: 'ngModel',
+        link: function(scope, element, attrs, ngModel) {
+          var validationKey = attrs.crmUiValidateName ? attrs.crmUiValidateName : 'crmUiValidate';
+          scope.$watch(attrs.crmUiValidate, function(newValue){
+            ngModel.$setValidity(validationKey, !!newValue);
+          });
+        }
+      };
+    })
+
     // like ng-show, but hides/displays elements using "visibility" which maintains positioning
     // example <div crm-ui-visible="false">...content...</div>
     .directive('crmUiVisible', function($parse) {