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

Commit 366e0c8

Browse files
committedAug 15, 2013
fix(typeahead): set validity flag for non-editable inputs
Closes #806
1 parent d34f2de commit 366e0c8

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed
 

‎src/typeahead/test/typeahead.spec.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,28 @@ describe('typeahead tests', function () {
163163
});
164164

165165
it('should support the editable property to limit model bindings to matches only', function () {
166-
var element = prepareInputEl("<div><input ng-model='result' typeahead='item for item in source | filter:$viewValue' typeahead-editable='false'></div>");
166+
var element = prepareInputEl("<div>ng-model='result' typeahead='item for item in source | filter:$viewValue' typeahead-editable='false'></div>");
167167
changeInputValueTo(element, 'not in matches');
168168
expect($scope.result).toEqual(undefined);
169169
});
170170

171+
it('should set validation erros for non-editable inputs', function () {
172+
173+
var element = prepareInputEl(
174+
"<div><form name='form'>" +
175+
"<input name='input' ng-model='result' typeahead='item for item in source | filter:$viewValue' typeahead-editable='false'>" +
176+
"</form></div>");
177+
178+
changeInputValueTo(element, 'not in matches');
179+
expect($scope.result).toEqual(undefined);
180+
expect($scope.form.input.$error.editable).toBeTruthy();
181+
182+
changeInputValueTo(element, 'foo');
183+
triggerKeyDown(element, 13);
184+
expect($scope.result).toEqual('foo');
185+
expect($scope.form.input.$error.editable).toBeFalsy();
186+
});
187+
171188
it('should bind loading indicator expression', inject(function ($timeout) {
172189

173190
$scope.isLoading = false;

‎src/typeahead/typeahead.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
2929
};
3030
}])
3131

32-
.directive('typeahead', ['$compile', '$parse', '$q', '$timeout', '$document', '$position', 'typeaheadParser', function ($compile, $parse, $q, $timeout, $document, $position, typeaheadParser) {
32+
.directive('typeahead', ['$compile', '$parse', '$q', '$timeout', '$document', '$position', 'typeaheadParser',
33+
function ($compile, $parse, $q, $timeout, $document, $position, typeaheadParser) {
3334

3435
var HOT_KEYS = [9, 13, 27, 38, 40];
3536

@@ -158,7 +159,12 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
158159
}
159160
}
160161

161-
return isEditable ? inputValue : undefined;
162+
if (isEditable) {
163+
return inputValue;
164+
} else {
165+
modelCtrl.$setValidity('editable', false);
166+
return undefined;
167+
}
162168
});
163169

164170
modelCtrl.$formatters.push(function (modelValue) {
@@ -192,15 +198,17 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
192198
locals[parserResult.itemName] = item = scope.matches[activeIdx].model;
193199
model = parserResult.modelMapper(originalScope, locals);
194200
$setModelValue(originalScope, model);
201+
modelCtrl.$setValidity('editable', true);
195202

196203
onSelectCallback(originalScope, {
197204
$item: item,
198205
$model: model,
199206
$label: parserResult.viewMapper(originalScope, locals)
200207
});
201208

202-
//return focus to the input element if a mach was selected via a mouse click event
203209
resetMatches();
210+
211+
//return focus to the input element if a mach was selected via a mouse click event
204212
element[0].focus();
205213
};
206214

0 commit comments

Comments
 (0)
This repository has been archived.