Permalink
Browse files

Merge pull request #472 from ovax3/master

Fix broken ui-validate-watch for retroactive dependencies
  • Loading branch information...
2 parents 0df9c30 + 5c72bfe commit b0d100b0408497f8c2aa42e011ffa0f169a74762 @ProLoser ProLoser committed Mar 26, 2013
Showing with 31 additions and 39 deletions.
  1. +2 −2 component.json
  2. +25 −22 modules/directives/validate/test/validateSpec.js
  3. +4 −15 modules/directives/validate/validate.js
View
@@ -2,7 +2,7 @@
"author": "AngularUI Team",
"name": "angular-ui",
"description": "AngularUI - Companion Suite for AngularJS",
- "version": "0.4.0",
+ "version": "0.4.1",
"homepage": "http://angular-ui.github.com",
"repository": {
"type": "git",
@@ -13,4 +13,4 @@
"jquery": ">= 1.8.0",
"angular": ">= 1.0.2"
}
-}
+}
@@ -1,5 +1,5 @@
describe('uiValidate', function ($compile) {
- var scope, compileAndDigest;
+ var scope, compileAndDigest, changeInputValue;
var trueValidator = function () {
return true;
@@ -26,6 +26,10 @@ describe('uiValidate', function ($compile) {
return inputElm;
};
+ changeInputValue = function(elm, value) {
+ elm.val(value);
+ elm.trigger('input');
+ };
}));
describe('initial validation', function () {
@@ -105,19 +109,19 @@ describe('uiValidate', function ($compile) {
scope.validateWatch = validateWatch;
});
- it('should watch the string and refire the single validator', function () {
+ it('should watch the model 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);
+ 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 () {
+ it('should watch the model 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);
+ 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);
@@ -127,26 +131,25 @@ describe('uiValidate', function ($compile) {
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');
+ it('should watch the model and refire the validation with the view $value', function () {
+ scope.value = "";
+ scope.watchMe = "bbb";
+ var input = compileAndDigest('<input name="input" ng-model="value" ui-validate="\'$value == watchMe\'" ui-validate-watch="watchMe">', scope);
+
+ // Initial state, form empty, invalid view value
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.$error.validator).toBe(true);
+ expect(scope.value).toBe(undefined); // Since the validator fails, the model is unset
+
+ changeInputValue(input, "aaa"); // The user types something invalid
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.$error.validator).toBe(true);
+ expect(scope.value).toBe(undefined); // Since the validator still fails, the model is still unset
+
+ scope.$apply('watchMe="aaa"'); // Now the watched model changes and matches the previously invalid view value
expect(scope.form.input.$valid).toBe(true);
- expect(scope.form.input.$error.foo).toBe(false);
- expect(scope.form.input.$error.bar).toBe(false);
+ expect(scope.form.input.$error.validator).toBe(false);
+ expect(scope.value).toBe("aaa"); // Since the validator succeeds, the model is set
});
});
@@ -20,7 +20,7 @@ angular.module('ui.directives').directive('uiValidate', function () {
restrict: 'A',
require: 'ngModel',
link: function (scope, elm, attrs, ctrl) {
- var validateFn, watch, validators = {},
+ var validateFn, watches, validators = {},
validateExpr = scope.$eval(attrs.uiValidate);
if (!validateExpr) return;
@@ -46,20 +46,9 @@ angular.module('ui.directives').directive('uiValidate', function () {
// 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);
- });
- });
- }
+ scope.$watch(attrs.uiValidateWatch, function() {
+ ctrl.$setViewValue(ctrl.$viewValue);
+ });
}
}
};

0 comments on commit b0d100b

Please sign in to comment.