Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 399016d

Browse files
devversionkara
authored andcommitted
update(autocomplete): md-require-match only turn invalid if search text is provided (#9119)
* Currently the `md-require-match` attribute checks whether an item is selected or not. The validity shouldn't be set to false, if the searchText is empty. Closes #9072. Breaking Change: The autocomplete validator `md-require-match` no longer matches if the search text is empty
1 parent 151378d commit 399016d

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

src/components/autocomplete/autocomplete.spec.js

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,7 +1609,10 @@ describe('<md-autocomplete>', function() {
16091609
var element = compile(template, scope);
16101610
var ctrl = element.find('md-autocomplete').controller('mdAutocomplete');
16111611

1612-
element.scope().searchText = 'fo';
1612+
// Flush the element gathering.
1613+
$timeout.flush();
1614+
1615+
scope.$apply('searchText = "fo"');
16131616
$timeout.flush();
16141617

16151618
ctrl.select(0);
@@ -1622,14 +1625,48 @@ describe('<md-autocomplete>', function() {
16221625

16231626
expect(scope.form.autocomplete.$error['md-require-match']).toBeFalsy();
16241627

1625-
ctrl.clear();
1628+
scope.$apply('searchText = "food"');
1629+
$timeout.flush();
16261630

1627-
scope.$apply();
1631+
expect(scope.searchText).toBe('food');
1632+
expect(scope.selectedItem).toBeNull();
1633+
expect(scope.form.autocomplete.$error['md-require-match']).toBeTruthy();
16281634

1629-
expect(scope.searchText).toBe('');
1630-
expect(scope.selectedItem).toBe(null);
1635+
}));
1636+
1637+
it('should not set to invalid if searchText is empty', inject(function($timeout) {
1638+
var scope = createScope();
1639+
var template = '\
1640+
<form name="form">\
1641+
<md-autocomplete\
1642+
md-input-name="autocomplete"\
1643+
md-selected-item="selectedItem"\
1644+
md-search-text="searchText"\
1645+
md-items="item in match(searchText)"\
1646+
md-item-text="item.display"\
1647+
placeholder="placeholder"\
1648+
md-require-match="true">\
1649+
<span md-highlight-text="searchText">{{item.display}}</span>\
1650+
</md-autocomplete>\
1651+
</form>';
1652+
1653+
compile(template, scope);
1654+
1655+
// Flush the element gathering.
1656+
$timeout.flush();
1657+
1658+
scope.$apply('searchText = "food"');
1659+
$timeout.flush();
1660+
1661+
expect(scope.searchText).toBe('food');
1662+
expect(scope.selectedItem).toBeNull();
16311663
expect(scope.form.autocomplete.$error['md-require-match']).toBeTruthy();
16321664

1665+
scope.$apply('searchText = ""');
1666+
1667+
expect(scope.searchText).toBe('');
1668+
expect(scope.selectedItem).toBeNull();
1669+
expect(scope.form.autocomplete.$error['md-require-match']).toBeFalsy();
16331670
}));
16341671

16351672
});

src/components/autocomplete/js/autocompleteController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
8484
function updateModelValidators() {
8585
if (!$scope.requireMatch || !inputModelCtrl) return;
8686

87-
inputModelCtrl.$setValidity('md-require-match', !!$scope.selectedItem);
87+
inputModelCtrl.$setValidity('md-require-match', !!$scope.selectedItem || !$scope.searchText);
8888
}
8989

9090
/**

0 commit comments

Comments
 (0)