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

Commit 61412b4

Browse files
committed
fix(select): form remains valid after empty option is selected
- change md-select's `$isEmpty()` implementation from a naive falsy check to a more robust check that is similar to AngularJS' default - add empty option to validations demo Fixes #10005
1 parent 5f8472c commit 61412b4

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/components/select/demoValidations/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<md-input-container flex="50">
1515
<label>Favorite Color</label>
1616
<md-select name="favoriteColor" ng-model="favoriteColor" required>
17+
<md-option value=""></md-option>
1718
<md-option value="red">Red</md-option>
1819
<md-option value="blue">Blue</md-option>
1920
<md-option value="green">Green</md-option>

src/components/select/select.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,8 @@ function SelectDirective($mdSelect, $mdUtil, $mdConstant, $mdTheming, $mdAria, $
589589

590590
function inputCheckValue() {
591591
// The select counts as having a value if one or more options are selected,
592-
// or if the input's validity state says it has bad input (eg string in a number input)
593-
// we must do this on nextTick as the $render is sometimes invoked on nextTick.
592+
// or if the input's validity state says it has bad input (eg: string in a number input).
593+
// We must do this on nextTick as the $render is sometimes invoked on nextTick.
594594
$mdUtil.nextTick(function () {
595595
containerCtrl && containerCtrl.setHasValue(
596596
selectMenuCtrl.getSelectedLabels().length > 0 || (element[0].validity || {}).badInput);
@@ -850,7 +850,10 @@ function SelectMenuDirective($parse, $mdUtil, $mdConstant, $mdTheming) {
850850
self.ngModel.$isEmpty = function($viewValue) {
851851
// We have to transform the viewValue into the hashKey, because otherwise the
852852
// OptionCtrl may not exist. Developers may have specified a trackBy function.
853-
return !self.options[self.hashGetter($viewValue)];
853+
var hashedValue = self.options[self.hashGetter($viewValue)] ? self.options[self.hashGetter($viewValue)].value : null;
854+
// Base this check on the default AngularJS $isEmpty() function.
855+
// eslint-disable-next-line no-self-compare
856+
return !angular.isDefined(hashedValue) || hashedValue === null || hashedValue === '' || hashedValue !== hashedValue;
854857
};
855858

856859
// Allow users to provide `ng-model="foo" ng-model-options="{trackBy: '$value.id'}"` so

0 commit comments

Comments
 (0)