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

Commit b004443

Browse files
andrenarchywesleycho
authored andcommitted
fix(typeahead): set validity if model is set manually
With this commit, the validity of a typeahead model is set to true if the model is set manually and the typehead directive is set to be non-editable. add test for setting model manually Fixes #3318
1 parent 03446c5 commit b004443

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/typeahead/test/typeahead.spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,22 @@ describe('typeahead tests', function () {
650650
$(match).click();
651651
$scope.$digest();
652652
});
653+
654+
it('issue #3318 - should set model validity to true when set manually', function () {
655+
656+
var element = prepareInputEl(
657+
'<div><form name="form">' +
658+
'<input name="input" ng-model="result" typeahead="item for item in source | filter:$viewValue" typeahead-editable="false">' +
659+
'</form></div>');
660+
661+
changeInputValueTo(element, 'not in matches');
662+
$scope.$apply(function () {
663+
$scope.result = 'manually set';
664+
});
665+
666+
expect($scope.result).toEqual('manually set');
667+
expect($scope.form.input.$valid).toBeTruthy();
668+
});
653669
});
654670

655671
describe('input formatting', function () {

src/typeahead/typeahead.js

+7
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
225225
var candidateViewValue, emptyViewValue;
226226
var locals = {};
227227

228+
// The validity may be set to false via $parsers (see above) if
229+
// the model is restricted to selected values. If the model
230+
// is set manually it is considered to be valid.
231+
if (!isEditable) {
232+
modelCtrl.$setValidity('editable', true);
233+
}
234+
228235
if (inputFormatter) {
229236

230237
locals.$model = modelValue;

0 commit comments

Comments
 (0)