Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
fix(typeahead): clear validity in $digest
Browse files Browse the repository at this point in the history
- Clear within $digest cycle

Closes #6033
Fixes #6032
  • Loading branch information
Siggen authored and wesleycho committed Jun 27, 2016
1 parent 1cbd73d commit ed3400b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
33 changes: 33 additions & 0 deletions src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,39 @@ describe('typeahead tests', function() {
expect($scope.form.input.$error.parse).toBeFalsy();
});

// fix for #6032
it('should clear errors and refresh scope after blur for typeahead-editable="false"', function () {
var element = prepareInputEl(
'<div><form name="form" ng-class="{invalid : form.input.$invalid}">' +
'<input name="input" ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-editable="false">' +
'</form></div>');
var inputEl = findInput(element);

// first try
changeInputValueTo(element, 'not in matches');
expect($scope.result).toEqual(undefined);
expect(inputEl.val()).toEqual('not in matches');
expect(element.find('form')).toHaveClass('invalid');
inputEl.blur();

expect(inputEl.val()).toEqual(''); // <-- input is reset
expect($scope.form.input.$error.editable).toBeFalsy();
expect($scope.form.input.$error.parse).toBeFalsy();
expect(element.find('form')).not.toHaveClass('invalid'); // <-- form has no error (it always works for some reason)

// second try
changeInputValueTo(element, 'not in matches');
expect($scope.result).toEqual(undefined);
expect(inputEl.val()).toEqual('not in matches');
expect(element.find('form')).toHaveClass('invalid');
inputEl.blur();

expect(inputEl.val()).toEqual(''); // <-- input is reset
expect($scope.form.input.$error.editable).toBeFalsy();
expect($scope.form.input.$error.parse).toBeFalsy();
expect(element.find('form')).not.toHaveClass('invalid'); // <-- form has no error (it didn't work prior to #6032 fix)
});

it('should go through other validators after blur for typeahead-editable="false"', function () {
var element = prepareInputEl(
'<div><form name="form">' +
Expand Down
8 changes: 5 additions & 3 deletions src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,11 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
}
if (!isEditable && modelCtrl.$error.editable) {
modelCtrl.$setViewValue();
// Reset validity as we are clearing
modelCtrl.$setValidity('editable', true);
modelCtrl.$setValidity('parse', true);
scope.$apply(function() {
// Reset validity as we are clearing
modelCtrl.$setValidity('editable', true);
modelCtrl.$setValidity('parse', true);
});
element.val('');
}
hasFocus = false;
Expand Down

0 comments on commit ed3400b

Please sign in to comment.