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

Commit

Permalink
perf(form): don't call $setSubmitted twice for nested child forms
Browse files Browse the repository at this point in the history
  • Loading branch information
Narretz committed Mar 6, 2017
1 parent 5dbb08e commit beb33a4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ng/directive/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ FormController.prototype = {
}

forEach(this.$$controls, function(control) {
if (control.$setSubmitted) {
if (control.$setSubmitted && !control.$submitted) {
control.$setSubmitted(true);
}
});
Expand Down
32 changes: 32 additions & 0 deletions test/ng/directive/formSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,9 +541,41 @@ describe('form', function() {
child = scope.child;

parent.$setSubmitted();
expect(parent.$submitted).toBeTruthy();
expect(child.$submitted).toBeTruthy();
});

it('should set $submitted to true on child and parent forms when form is submitted', function() {
doc = jqLite(
'<ng-form name="parent">' +
'<ng-form name="child">' +
'<ng-form name="grandchild">' +
'<input ng:model="modelA" name="inputA">' +
'<input ng:model="modelB" name="inputB">' +
'</ng-form>' +
'</ng-form>' +
'</ng-form>');
$compile(doc)(scope);

var parent = scope.parent,
child = scope.child,
grandchild = scope.grandchild;

spyOn(parent, '$setSubmitted').and.callThrough();
spyOn(child, '$setSubmitted').and.callThrough();
spyOn(grandchild, '$setSubmitted').and.callThrough();

child.$setSubmitted();

expect(parent.$submitted).toBeTruthy();
expect(child.$submitted).toBeTruthy();
expect(grandchild.$submitted).toBeTruthy();

expect(parent.$setSubmitted).toHaveBeenCalledOnce();
expect(child.$setSubmitted).toHaveBeenCalledOnce();
expect(grandchild.$setSubmitted).toHaveBeenCalledOnce();
});

it('should deregister a child form when its DOM is removed', function() {
doc = jqLite(
'<form name="parent">' +
Expand Down

0 comments on commit beb33a4

Please sign in to comment.