Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
fix(ngModel): ensure validation occurs when the model value changes u…
Browse files Browse the repository at this point in the history
…pon digest
  • Loading branch information
matsko committed Feb 14, 2014
1 parent 9a1ef31 commit f34e0b3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/directive/ng_model.dart
Expand Up @@ -28,6 +28,10 @@ class NgModel extends NgControl implements NgAttachAware {
_exp = 'ng-model=${attrs["ng-model"]}';
}

process(value) {
render(value);
validate();
}

attach() {
watchCollection = false;
Expand All @@ -51,9 +55,9 @@ class NgModel extends NgControl implements NgAttachAware {
_watchCollection = value;
_removeWatch();
if (_watchCollection) {
_removeWatch = _scope.$watchCollection((s) => getter(), (value) => render(value), _exp);
_removeWatch = _scope.$watchCollection((s) => getter(), (value) => process(value), _exp);
} else {
_removeWatch = _scope.$watch((s) => getter(), (value) => render(value), _exp);
_removeWatch = _scope.$watch((s) => getter(), (value) => process(value), _exp);
}
}

Expand Down
35 changes: 35 additions & 0 deletions test/directive/ng_model_spec.dart
Expand Up @@ -758,6 +758,41 @@ describe('ng-model', () {
}));
});

describe('validation', () {
it('should happen automatically when the scope changes', inject((Scope scope) {
_.compile('<input type="text" ng-model="model" probe="i" required>');
_.rootScope.$digest();

Probe probe = _.rootScope.i;
var model = probe.directive(NgModel);

expect(model.invalid).toBe(true);
expect(model.valid).toBe(false);

_.rootScope.$apply('model = "viljami"');

expect(model.invalid).toBe(false);
expect(model.valid).toBe(true);
}));

it('should happen automatically upon user input via the onInput event', inject(() {
_.compile('<input type="text" ng-model="model" probe="i" required>');

Probe probe = _.rootScope.i;
var model = probe.directive(NgModel);
InputElement inputElement = model.element;

expect(model.invalid).toBe(true);
expect(model.valid).toBe(false);

inputElement.value = 'some value';
_.triggerEvent(inputElement, 'input');

expect(model.invalid).toBe(false);
expect(model.valid).toBe(true);
}));
});

describe('valid / invalid', () {
it('should add and remove the correct flags when set to valid and to invalid', inject((Scope scope) {
_.compile('<input type="text" ng-model="my_model" probe="i" />');
Expand Down

0 comments on commit f34e0b3

Please sign in to comment.