From 19966a31123a1b4a86e980c95366bb3703590dbe Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Sat, 30 Jan 2016 11:18:48 +0100 Subject: [PATCH] fix(select): fix set form to pristine if ng-model for multiple select is predefined. Bug introduced in 09bd5a3b52f29dfe530192099d2c405f204ec3ad Fixes #6556 Closes #6782 #breaking --- src/components/select/select.js | 7 +++++-- src/components/select/select.spec.js | 27 ++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/components/select/select.js b/src/components/select/select.js index ffe56eb5edd..e2686ea6da8 100755 --- a/src/components/select/select.js +++ b/src/components/select/select.js @@ -230,9 +230,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(); + } }); } diff --git a/src/components/select/select.spec.js b/src/components/select/select.spec.js index 5dd3982e251..dda0c48b579 100755 --- a/src/components/select/select.spec.js +++ b/src/components/select/select.spec.js @@ -387,6 +387,19 @@ describe('', 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('
' + + '' + + '' + + '
')($rootScope); + $rootScope.$digest(); + $timeout.flush(); + + expect($rootScope.testForm.$pristine).toBe(true); + })); + }); describe('view->model', function() { @@ -637,13 +650,25 @@ describe('', function() { $rootScope.model = []; $rootScope.opts = [1, 2, 3, 4]; $compile('
' + - '' + + '' + '' + '')($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('
' + + '' + + '' + + '
')($rootScope); + $rootScope.$digest(); + $timeout.flush(); + + expect($rootScope.testForm.$pristine).toBe(true); + })); }); describe('view->model', function() {