From b004443371d75bb61e9a17a00a58b485c1279aee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Gaul?= Date: Fri, 20 Feb 2015 13:47:07 +0100 Subject: [PATCH] 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 --- src/typeahead/test/typeahead.spec.js | 16 ++++++++++++++++ src/typeahead/typeahead.js | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/src/typeahead/test/typeahead.spec.js b/src/typeahead/test/typeahead.spec.js index 7b54ab7b8f..a8d2d16ace 100644 --- a/src/typeahead/test/typeahead.spec.js +++ b/src/typeahead/test/typeahead.spec.js @@ -650,6 +650,22 @@ describe('typeahead tests', function () { $(match).click(); $scope.$digest(); }); + + it('issue #3318 - should set model validity to true when set manually', function () { + + var element = prepareInputEl( + '
' + + '' + + '
'); + + changeInputValueTo(element, 'not in matches'); + $scope.$apply(function () { + $scope.result = 'manually set'; + }); + + expect($scope.result).toEqual('manually set'); + expect($scope.form.input.$valid).toBeTruthy(); + }); }); describe('input formatting', function () { diff --git a/src/typeahead/typeahead.js b/src/typeahead/typeahead.js index fa27320fa4..e47b6dae5e 100644 --- a/src/typeahead/typeahead.js +++ b/src/typeahead/typeahead.js @@ -225,6 +225,13 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap var candidateViewValue, emptyViewValue; var locals = {}; + // The validity may be set to false via $parsers (see above) if + // the model is restricted to selected values. If the model + // is set manually it is considered to be valid. + if (!isEditable) { + modelCtrl.$setValidity('editable', true); + } + if (inputFormatter) { locals.$model = modelValue;