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

Commit 4f94819

Browse files
committed
perf(form): don't call $setSubmitted twice for nested child forms
1 parent 28f69af commit 4f94819

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/ng/directive/form.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ FormController.prototype = {
286286
}
287287

288288
forEach(this.$$controls, function(control) {
289-
if (control.$setSubmitted) {
289+
if (control.$setSubmitted && !control.$submitted) {
290290
control.$setSubmitted(true);
291291
}
292292
});

test/ng/directive/formSpec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,41 @@ describe('form', function() {
553553
child = scope.child;
554554

555555
parent.$setSubmitted();
556+
expect(parent.$submitted).toBeTruthy();
556557
expect(child.$submitted).toBeTruthy();
557558
});
558559

560+
it('should set $submitted to true on child and parent forms when form is submitted', function() {
561+
doc = jqLite(
562+
'<ng-form name="parent">' +
563+
'<ng-form name="child">' +
564+
'<ng-form name="grandchild">' +
565+
'<input ng:model="modelA" name="inputA">' +
566+
'<input ng:model="modelB" name="inputB">' +
567+
'</ng-form>' +
568+
'</ng-form>' +
569+
'</ng-form>');
570+
$compile(doc)(scope);
571+
572+
var parent = scope.parent,
573+
child = scope.child,
574+
grandchild = scope.grandchild;
575+
576+
spyOn(parent, '$setSubmitted').and.callThrough();
577+
spyOn(child, '$setSubmitted').and.callThrough();
578+
spyOn(grandchild, '$setSubmitted').and.callThrough();
579+
580+
child.$setSubmitted();
581+
582+
expect(parent.$submitted).toBeTruthy();
583+
expect(child.$submitted).toBeTruthy();
584+
expect(grandchild.$submitted).toBeTruthy();
585+
586+
expect(parent.$setSubmitted).toHaveBeenCalledOnce();
587+
expect(child.$setSubmitted).toHaveBeenCalledOnce();
588+
expect(grandchild.$setSubmitted).toHaveBeenCalledOnce();
589+
});
590+
559591
it('should deregister a child form when its DOM is removed', function() {
560592
doc = jqLite(
561593
'<form name="parent">' +

0 commit comments

Comments
 (0)