summaryrefslogtreecommitdiffstats
path: root/js/vendor/angular-ui/modules/directives/validate
diff options
context:
space:
mode:
Diffstat (limited to 'js/vendor/angular-ui/modules/directives/validate')
-rw-r--r--js/vendor/angular-ui/modules/directives/validate/test/validateSpec.js164
-rw-r--r--js/vendor/angular-ui/modules/directives/validate/validate.js66
2 files changed, 230 insertions, 0 deletions
diff --git a/js/vendor/angular-ui/modules/directives/validate/test/validateSpec.js b/js/vendor/angular-ui/modules/directives/validate/test/validateSpec.js
new file mode 100644
index 000000000..25a685c4f
--- /dev/null
+++ b/js/vendor/angular-ui/modules/directives/validate/test/validateSpec.js
@@ -0,0 +1,164 @@
+describe('uiValidate', function ($compile) {
+ var scope, compileAndDigest;
+
+ var trueValidator = function () {
+ return true;
+ };
+
+ var falseValidator = function () {
+ return false;
+ };
+
+ var passedValueValidator = function (valueToValidate) {
+ return valueToValidate;
+ };
+
+ beforeEach(module('ui'));
+ beforeEach(inject(function ($rootScope, $compile) {
+
+ scope = $rootScope.$new();
+ compileAndDigest = function (inputHtml, scope) {
+ var inputElm = angular.element(inputHtml);
+ var formElm = angular.element('<form name="form"></form>');
+ formElm.append(inputElm);
+ $compile(formElm)(scope);
+ scope.$digest();
+
+ return inputElm;
+ };
+ }));
+
+ describe('initial validation', function () {
+
+ it('should mark input as valid if initial model is valid', inject(function () {
+
+ scope.validate = trueValidator;
+ compileAndDigest('<input name="input" ng-model="value" ui-validate="\'validate($value)\'">', scope);
+ expect(scope.form.input.$valid).toBeTruthy();
+ expect(scope.form.input.$error).toEqual({validator: false});
+ }));
+
+ it('should mark input as invalid if initial model is invalid', inject(function () {
+
+ scope.validate = falseValidator;
+ compileAndDigest('<input name="input" ng-model="value" ui-validate="\'validate($value)\'">', scope);
+ expect(scope.form.input.$valid).toBeFalsy();
+ expect(scope.form.input.$error).toEqual({ validator: true });
+ }));
+ });
+
+ describe('validation on model change', function () {
+
+ it('should change valid state in response to model changes', inject(function () {
+
+ scope.value = false;
+ scope.validate = passedValueValidator;
+ compileAndDigest('<input name="input" ng-model="value" ui-validate="\'validate($value)\'">', scope);
+ expect(scope.form.input.$valid).toBeFalsy();
+
+ scope.$apply('value = true');
+ expect(scope.form.input.$valid).toBeTruthy();
+ }));
+ });
+
+ describe('validation on element change', function () {
+
+ var sniffer;
+ beforeEach(inject(function ($sniffer) {
+ sniffer = $sniffer;
+ }));
+
+ it('should change valid state in response to element events', function () {
+
+ scope.value = false;
+ scope.validate = passedValueValidator;
+ var inputElm = compileAndDigest('<input name="input" ng-model="value" ui-validate="\'validate($value)\'">', scope);
+ expect(scope.form.input.$valid).toBeFalsy();
+
+ inputElm.val('true');
+ inputElm.trigger((sniffer.hasEvent('input') ? 'input' : 'change'));
+
+ expect(scope.form.input.$valid).toBeTruthy();
+ });
+ });
+
+ describe('multiple validators with custom keys', function () {
+
+ it('should support multiple validators with custom keys', function () {
+
+ scope.validate1 = trueValidator;
+ scope.validate2 = falseValidator;
+
+ compileAndDigest('<input name="input" ng-model="value" ui-validate="{key1 : \'validate1($value)\', key2 : \'validate2($value)\'}">', scope);
+ expect(scope.form.input.$valid).toBeFalsy();
+ expect(scope.form.input.$error.key1).toBeFalsy();
+ expect(scope.form.input.$error.key2).toBeTruthy();
+ });
+ });
+
+ describe('uiValidateWatch', function(){
+ function validateWatch(watchMe) {
+ return watchMe;
+ };
+
+ beforeEach(function(){
+ scope.validateWatch = validateWatch;
+ });
+
+ it('should watch the string and refire the single validator', function () {
+ scope.watchMe = false;
+ compileAndDigest('<input name="input" ng-model="value" ui-validate="\'validateWatch(watchMe)\'" ui-validate-watch="\'watchMe\'">', scope);
+ expect(scope.form.input.$valid).toBe(false);
+ expect(scope.form.input.$error.validator).toBe(true);
+ scope.$apply('watchMe=true');
+ expect(scope.form.input.$valid).toBe(true);
+ expect(scope.form.input.$error.validator).toBe(false);
+ });
+
+ it('should watch the string and refire all validators', function () {
+ scope.watchMe = false;
+ compileAndDigest('<input name="input" ng-model="value" ui-validate="{foo:\'validateWatch(watchMe)\',bar:\'validateWatch(watchMe)\'}" ui-validate-watch="\'watchMe\'">', scope);
+ expect(scope.form.input.$valid).toBe(false);
+ expect(scope.form.input.$error.foo).toBe(true);
+ expect(scope.form.input.$error.bar).toBe(true);
+ scope.$apply('watchMe=true');
+ expect(scope.form.input.$valid).toBe(true);
+ expect(scope.form.input.$error.foo).toBe(false);
+ expect(scope.form.input.$error.bar).toBe(false);
+ });
+
+ it('should watch the all object attributes and each respective validator', function () {
+ scope.watchFoo = false;
+ scope.watchBar = false;
+ compileAndDigest('<input name="input" ng-model="value" ui-validate="{foo:\'validateWatch(watchFoo)\',bar:\'validateWatch(watchBar)\'}" ui-validate-watch="{foo:\'watchFoo\',bar:\'watchBar\'}">', scope);
+ expect(scope.form.input.$valid).toBe(false);
+ expect(scope.form.input.$error.foo).toBe(true);
+ expect(scope.form.input.$error.bar).toBe(true);
+ scope.$apply('watchFoo=true');
+ expect(scope.form.input.$valid).toBe(false);
+ expect(scope.form.input.$error.foo).toBe(false);
+ expect(scope.form.input.$error.bar).toBe(true);
+ scope.$apply('watchBar=true');
+ scope.$apply('watchFoo=false');
+ expect(scope.form.input.$valid).toBe(false);
+ expect(scope.form.input.$error.foo).toBe(true);
+ expect(scope.form.input.$error.bar).toBe(false);
+ scope.$apply('watchFoo=true');
+ expect(scope.form.input.$valid).toBe(true);
+ expect(scope.form.input.$error.foo).toBe(false);
+ expect(scope.form.input.$error.bar).toBe(false);
+ });
+
+ });
+
+ describe('error cases', function () {
+ it('should fail if ngModel not present', inject(function () {
+ expect(function () {
+ compileAndDigest('<input name="input" ui-validate="\'validate($value)\'">', scope);
+ }).toThrow(new Error('No controller: ngModel'));
+ }));
+ it('should have no effect if validate expression is empty', inject(function () {
+ compileAndDigest('<input ng-model="value" ui-validate="">', scope);
+ }));
+ });
+});
diff --git a/js/vendor/angular-ui/modules/directives/validate/validate.js b/js/vendor/angular-ui/modules/directives/validate/validate.js
new file mode 100644
index 000000000..2d255a60e
--- /dev/null
+++ b/js/vendor/angular-ui/modules/directives/validate/validate.js
@@ -0,0 +1,66 @@
+/**
+ * General-purpose validator for ngModel.
+ * angular.js comes with several built-in validation mechanism for input fields (ngRequired, ngPattern etc.) but using
+ * an arbitrary validation function requires creation of a custom formatters and / or parsers.
+ * The ui-validate directive makes it easy to use any function(s) defined in scope as a validator function(s).
+ * A validator function will trigger validation on both model and input changes.
+ *
+ * @example <input ui-validate=" 'myValidatorFunction($value)' ">
+ * @example <input ui-validate="{ foo : '$value > anotherModel', bar : 'validateFoo($value)' }">
+ * @example <input ui-validate="{ foo : '$value > anotherModel' }" ui-validate-watch=" 'anotherModel' ">
+ * @example <input ui-validate="{ foo : '$value > anotherModel', bar : 'validateFoo($value)' }" ui-validate-watch=" { foo : 'anotherModel' } ">
+ *
+ * @param ui-validate {string|object literal} If strings is passed it should be a scope's function to be used as a validator.
+ * If an object literal is passed a key denotes a validation error key while a value should be a validator function.
+ * In both cases validator function should take a value to validate as its argument and should return true/false indicating a validation result.
+ */
+angular.module('ui.directives').directive('uiValidate', function () {
+
+ return {
+ restrict: 'A',
+ require: 'ngModel',
+ link: function (scope, elm, attrs, ctrl) {
+ var validateFn, watch, validators = {},
+ validateExpr = scope.$eval(attrs.uiValidate);
+
+ if (!validateExpr) return;
+
+ if (angular.isString(validateExpr)) {
+ validateExpr = { validator: validateExpr };
+ }
+
+ angular.forEach(validateExpr, function (expression, key) {
+ validateFn = function (valueToValidate) {
+ if (scope.$eval(expression, { '$value' : valueToValidate })) {
+ ctrl.$setValidity(key, true);
+ return valueToValidate;
+ } else {
+ ctrl.$setValidity(key, false);
+ return undefined;
+ }
+ };
+ validators[key] = validateFn;
+ ctrl.$formatters.push(validateFn);
+ ctrl.$parsers.push(validateFn);
+ });
+
+ // Support for ui-validate-watch
+ if (attrs.uiValidateWatch) {
+ watch = scope.$eval(attrs.uiValidateWatch);
+ if (angular.isString(watch)) {
+ scope.$watch(watch, function(){
+ angular.forEach(validators, function(validatorFn, key){
+ validatorFn(ctrl.$modelValue);
+ });
+ });
+ } else {
+ angular.forEach(watch, function(expression, key){
+ scope.$watch(expression, function(){
+ validators[key](ctrl.$modelValue);
+ });
+ });
+ }
+ }
+ }
+ };
+});