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

fix(ngModel): form validation and Object.prototype enumerable values #12075

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/ng/directive/ngModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,9 @@ function addSetValidityMethod(context) {
function isObjectEmpty(obj) {
if (obj) {
for (var prop in obj) {
return false;
if (obj.hasOwnProperty(prop)) {
return false;
}
}
}
return true;
Expand Down
25 changes: 25 additions & 0 deletions test/ng/directive/ngModelSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,31 @@ describe('ngModel', function() {
}));


it('should be possible to extend Object prototype and still be able to do form validation',
inject(function($compile, $rootScope) {
Object.prototype.someThing = function() {};
var element = $compile('<form name="myForm">' +
'<input type="text" name="username" ng-model="username" minlength="10" required />' +
'</form>')($rootScope);
var inputElm = element.find('input');

var formCtrl = $rootScope.myForm;
var usernameCtrl = formCtrl.username;

$rootScope.$digest();
expect(usernameCtrl.$invalid).toBe(true);
expect(formCtrl.$invalid).toBe(true);

usernameCtrl.$setViewValue('valid-username');
$rootScope.$digest();

expect(usernameCtrl.$invalid).toBe(false);
expect(formCtrl.$invalid).toBe(false);
delete Object.prototype.someThing;

dealoc(element);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theoretically not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and in practice, generates the error "Error: Found jqCache references that were not deallocated! count: 1"

Keep in mind that this is a new element (not the global one)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right !
Why not reuse the global element then ? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The global element is already compiled with a content that is not good for this test. This same thing is done for other tests in the same file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have taken a more detailed look :)
(Sorry for the noise...)

}));

it('should re-evaluate the form validity state once the asynchronous promise has been delivered',
inject(function($compile, $rootScope, $q) {

Expand Down