Skip to content

Commit 5dbb08e

Browse files
Caleb KniffenNarretz
Caleb Kniffen
authored andcommitted
fix(form): set $submitted to true on child forms when parent is submitted.
Closes angular#10071
1 parent 603b66e commit 5dbb08e

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/ng/directive/form.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,22 @@ FormController.prototype = {
268268
* @name form.FormController#$setSubmitted
269269
*
270270
* @description
271-
* Sets the form to its submitted state.
271+
* Sets the form to its `$submitted` state. This will also set `$submitted` on all child and
272+
* parent forms of the form.
272273
*/
273-
$setSubmitted: function() {
274-
this.$$animate.addClass(this.$$element, SUBMITTED_CLASS);
275-
this.$submitted = true;
276-
this.$$parentForm.$setSubmitted();
274+
$setSubmitted: function(onlySetOnChildren) {
275+
this.$$animate.addClass(this.$$element, SUBMITTED_CLASS);
276+
this.$submitted = true;
277+
278+
if (!onlySetOnChildren) {
279+
this.$$parentForm.$setSubmitted();
280+
}
281+
282+
forEach(this.$$controls, function(control) {
283+
if (control.$setSubmitted) {
284+
control.$setSubmitted(true);
285+
}
286+
});
277287
}
278288
};
279289

test/ng/directive/formSpec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,22 @@ describe('form', function() {
527527
expect(parent.$submitted).toBeTruthy();
528528
});
529529

530+
it('should set $submitted to true on child forms when parent is submitted', function() {
531+
doc = jqLite(
532+
'<ng-form name="parent">' +
533+
'<ng-form name="child">' +
534+
'<input ng:model="modelA" name="inputA">' +
535+
'<input ng:model="modelB" name="inputB">' +
536+
'</ng-form>' +
537+
'</ng-form>');
538+
$compile(doc)(scope);
539+
540+
var parent = scope.parent,
541+
child = scope.child;
542+
543+
parent.$setSubmitted();
544+
expect(child.$submitted).toBeTruthy();
545+
});
530546

531547
it('should deregister a child form when its DOM is removed', function() {
532548
doc = jqLite(

0 commit comments

Comments
 (0)