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

Commit 19966a3

Browse files
devversionThomasBurleson
authored andcommitted
fix(select): fix set form to pristine if ng-model for multiple select is predefined.
Bug introduced in 09bd5a3 Fixes #6556 Closes #6782 #breaking
1 parent 4d535e2 commit 19966a3

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/components/select/select.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,12 @@ function SelectDirective($mdSelect, $mdUtil, $mdTheming, $mdAria, $compile, $par
230230
});
231231
}
232232

233-
if (formCtrl) {
233+
if (formCtrl && angular.isDefined(attr.multiple)) {
234234
$mdUtil.nextTick(function() {
235-
formCtrl.$setPristine();
235+
var hasModelValue = ngModelCtrl.$modelValue || ngModelCtrl.$viewValue;
236+
if (hasModelValue) {
237+
formCtrl.$setPristine();
238+
}
236239
});
237240
}
238241

src/components/select/select.spec.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,19 @@ describe('<md-select>', function() {
387387
expect(selectedOptions(el).length).toBe(0);
388388
}));
389389

390+
it('should keep the form pristine when model is predefined', inject(function($rootScope, $timeout, $compile) {
391+
$rootScope.model = 2;
392+
$rootScope.opts = [1, 2, 3, 4];
393+
$compile('<form name="testForm">' +
394+
'<md-select ng-model="model" name="multiSelect">' +
395+
'<md-option ng-repeat="opt in opts" ng-value="opt"></md-option>' +
396+
'</md-select></form>')($rootScope);
397+
$rootScope.$digest();
398+
$timeout.flush();
399+
400+
expect($rootScope.testForm.$pristine).toBe(true);
401+
}));
402+
390403
});
391404

392405
describe('view->model', function() {
@@ -637,13 +650,25 @@ describe('<md-select>', function() {
637650
$rootScope.model = [];
638651
$rootScope.opts = [1, 2, 3, 4];
639652
$compile('<form name="testForm">' +
640-
'<md-select ng-model="model", name="multiSelect" required="required" multiple="multiple">' +
653+
'<md-select ng-model="model" name="multiSelect" required="required" multiple="multiple">' +
641654
'<md-option ng-repeat="opt in opts" ng-value="opt"></md-option>' +
642655
'</md-select></form>')($rootScope);
643656
$rootScope.$digest();
644657
expect($rootScope.testForm.$valid).toBe(false);
645658
}));
646659

660+
it('should keep the form pristine when model is predefined', inject(function($rootScope, $timeout, $compile) {
661+
$rootScope.model = [1, 2];
662+
$rootScope.opts = [1, 2, 3, 4];
663+
$compile('<form name="testForm">' +
664+
'<md-select ng-model="model" name="multiSelect" multiple="multiple">' +
665+
'<md-option ng-repeat="opt in opts" ng-value="opt"></md-option>' +
666+
'</md-select></form>')($rootScope);
667+
$rootScope.$digest();
668+
$timeout.flush();
669+
670+
expect($rootScope.testForm.$pristine).toBe(true);
671+
}));
647672
});
648673

649674
describe('view->model', function() {

0 commit comments

Comments
 (0)