Skip to content

Commit

Permalink
fix(select): fix set form to pristine if ng-model for multiple select…
Browse files Browse the repository at this point in the history
… is predefined.

Bug introduced in 09bd5a3

Fixes angular#6556
  • Loading branch information
devversion committed Jan 30, 2016
1 parent bf78b0c commit 531bc5c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/components/select/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,12 @@ function SelectDirective($mdSelect, $mdUtil, $mdTheming, $mdAria, $compile, $par
});
}

if (formCtrl) {
if (formCtrl && angular.isDefined(attr.multiple)) {
$mdUtil.nextTick(function() {
formCtrl.$setPristine();
var hasModelValue = ngModelCtrl.$modelValue || ngModelCtrl.$viewValue;
if (hasModelValue) {
formCtrl.$setPristine();
}
});
}

Expand Down
27 changes: 26 additions & 1 deletion src/components/select/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,19 @@ describe('<md-select>', function() {
expect(selectedOptions(el).length).toBe(0);
}));

it('should keep the form pristine when model is predefined', inject(function($rootScope, $timeout, $compile) {
$rootScope.model = 2;
$rootScope.opts = [1, 2, 3, 4];
$compile('<form name="testForm">' +
'<md-select ng-model="model" name="multiSelect">' +
'<md-option ng-repeat="opt in opts" ng-value="opt"></md-option>' +
'</md-select></form>')($rootScope);
$rootScope.$digest();
$timeout.flush();

expect($rootScope.testForm.$pristine).toBe(true);
}));

});

describe('view->model', function() {
Expand Down Expand Up @@ -637,13 +650,25 @@ describe('<md-select>', function() {
$rootScope.model = [];
$rootScope.opts = [1, 2, 3, 4];
$compile('<form name="testForm">' +
'<md-select ng-model="model", name="multiSelect" required="required" multiple="multiple">' +
'<md-select ng-model="model" name="multiSelect" required="required" multiple="multiple">' +
'<md-option ng-repeat="opt in opts" ng-value="opt"></md-option>' +
'</md-select></form>')($rootScope);
$rootScope.$digest();
expect($rootScope.testForm.$valid).toBe(false);
}));

it('should keep the form pristine when model is predefined', inject(function($rootScope, $timeout, $compile) {
$rootScope.model = [1, 2];
$rootScope.opts = [1, 2, 3, 4];
$compile('<form name="testForm">' +
'<md-select ng-model="model" name="multiSelect" multiple="multiple">' +
'<md-option ng-repeat="opt in opts" ng-value="opt"></md-option>' +
'</md-select></form>')($rootScope);
$rootScope.$digest();
$timeout.flush();

expect($rootScope.testForm.$pristine).toBe(true);
}));
});

describe('view->model', function() {
Expand Down

0 comments on commit 531bc5c

Please sign in to comment.