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

Commit beb33a4

Browse files
committed
perf(form): don't call $setSubmitted twice for nested child forms
1 parent 5dbb08e commit beb33a4

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
@@ -280,7 +280,7 @@ FormController.prototype = {
280280
}
281281

282282
forEach(this.$$controls, function(control) {
283-
if (control.$setSubmitted) {
283+
if (control.$setSubmitted && !control.$submitted) {
284284
control.$setSubmitted(true);
285285
}
286286
});

test/ng/directive/formSpec.js

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

543543
parent.$setSubmitted();
544+
expect(parent.$submitted).toBeTruthy();
544545
expect(child.$submitted).toBeTruthy();
545546
});
546547

548+
it('should set $submitted to true on child and parent forms when form is submitted', function() {
549+
doc = jqLite(
550+
'<ng-form name="parent">' +
551+
'<ng-form name="child">' +
552+
'<ng-form name="grandchild">' +
553+
'<input ng:model="modelA" name="inputA">' +
554+
'<input ng:model="modelB" name="inputB">' +
555+
'</ng-form>' +
556+
'</ng-form>' +
557+
'</ng-form>');
558+
$compile(doc)(scope);
559+
560+
var parent = scope.parent,
561+
child = scope.child,
562+
grandchild = scope.grandchild;
563+
564+
spyOn(parent, '$setSubmitted').and.callThrough();
565+
spyOn(child, '$setSubmitted').and.callThrough();
566+
spyOn(grandchild, '$setSubmitted').and.callThrough();
567+
568+
child.$setSubmitted();
569+
570+
expect(parent.$submitted).toBeTruthy();
571+
expect(child.$submitted).toBeTruthy();
572+
expect(grandchild.$submitted).toBeTruthy();
573+
574+
expect(parent.$setSubmitted).toHaveBeenCalledOnce();
575+
expect(child.$setSubmitted).toHaveBeenCalledOnce();
576+
expect(grandchild.$setSubmitted).toHaveBeenCalledOnce();
577+
});
578+
547579
it('should deregister a child form when its DOM is removed', function() {
548580
doc = jqLite(
549581
'<form name="parent">' +

0 commit comments

Comments
 (0)