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

Commit

Permalink
fix(typeahead): use $setViewValue on blur
Browse files Browse the repository at this point in the history
Use $setViewValue instead of setting directly $viewValue to '', to make new value go through validators.

Closes #5769
Fixes #5694
  • Loading branch information
pdesgarets authored and wesleycho committed Apr 7, 2016
1 parent 3d7191c commit bba3f27
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,21 @@ describe('typeahead tests', function() {
expect($scope.form.input.$error.parse).toBeFalsy();
});

it('should go through other validators after blur for typeahead-editable="false"', function () {
var element = prepareInputEl(
'<div><form name="form">' +
'<input name="input" ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-editable="false" required>' +
'</form></div>');
var inputEl = findInput(element);

changeInputValueTo(element, 'not in matches');
expect($scope.result).toEqual(undefined);
expect(inputEl.val()).toEqual('not in matches');
inputEl.blur(); // input loses focus
expect($scope.result).toEqual(undefined);
expect($scope.form.input.$error.required).toBeTruthy();
});

it('should clear view value when no value selected for typeahead-editable="false" typeahead-select-on-blur="false"', function () {
var element = prepareInputEl('<div><input ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-editable="false" typeahead-select-on-blur="false"></div>');
var inputEl = findInput(element);
Expand Down
2 changes: 1 addition & 1 deletion src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
});
}
if (!isEditable && modelCtrl.$error.editable) {
modelCtrl.$viewValue = '';
modelCtrl.$setViewValue();
// Reset validity as we are clearing
modelCtrl.$setValidity('editable', true);
modelCtrl.$setValidity('parse', true);
Expand Down

0 comments on commit bba3f27

Please sign in to comment.