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

Commit 0934b76

Browse files
committed
fix(ngModel): form validation when there is an Object.prototype enumerable value
When adding an Object.prototype enumerable property, this should not be confused as a form error Closes #12066
1 parent 571bee7 commit 0934b76

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/ng/directive/ngModel.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,9 @@ function addSetValidityMethod(context) {
13511351
function isObjectEmpty(obj) {
13521352
if (obj) {
13531353
for (var prop in obj) {
1354-
return false;
1354+
if (obj.hasOwnProperty(prop)) {
1355+
return false;
1356+
}
13551357
}
13561358
}
13571359
return true;

test/ng/directive/ngModelSpec.js

+25
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,31 @@ describe('ngModel', function() {
10911091
}));
10921092

10931093

1094+
it('should be possible to extend Object prototype and still be able to do form validation',
1095+
inject(function($compile, $rootScope) {
1096+
Object.prototype.someThing = function() {};
1097+
var element = $compile('<form name="myForm">' +
1098+
'<input type="text" name="username" ng-model="username" minlength="10" required />' +
1099+
'</form>')($rootScope);
1100+
var inputElm = element.find('input');
1101+
1102+
var formCtrl = $rootScope.myForm;
1103+
var usernameCtrl = formCtrl.username;
1104+
1105+
$rootScope.$digest();
1106+
expect(usernameCtrl.$invalid).toBe(true);
1107+
expect(formCtrl.$invalid).toBe(true);
1108+
1109+
usernameCtrl.$setViewValue('valid-username');
1110+
$rootScope.$digest();
1111+
1112+
expect(usernameCtrl.$invalid).toBe(false);
1113+
expect(formCtrl.$invalid).toBe(false);
1114+
delete Object.prototype.someThing;
1115+
1116+
dealoc(element);
1117+
}));
1118+
10941119
it('should re-evaluate the form validity state once the asynchronous promise has been delivered',
10951120
inject(function($compile, $rootScope, $q) {
10961121

0 commit comments

Comments
 (0)