Skip to content

Commit ca613d8

Browse files
author
Caleb Kniffen
committed
fix(form): set $submitted to true on child forms when parent is submitted.
FormController will now call $setSubmitted on its child forms when it is submitted. Closes angular#10071
1 parent 2d0eda1 commit ca613d8

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/ng/directive/form.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,17 @@ function FormController(element, attrs, $scope, $animate, $interpolate) {
276276
* @description
277277
* Sets the form to its submitted state.
278278
*/
279-
form.$setSubmitted = function() {
279+
form.$setSubmitted = function(reverse) {
280280
$animate.addClass(element, SUBMITTED_CLASS);
281281
form.$submitted = true;
282-
parentForm.$setSubmitted();
282+
if(!reverse){
283+
parentForm.$setSubmitted();
284+
}
285+
forEach(controls, function(control) {
286+
if(control.$setSubmitted){
287+
control.$setSubmitted(true);
288+
}
289+
});
283290
};
284291
}
285292

test/ng/directive/formSpec.js

+16
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,22 @@ describe('form', function() {
410410
expect(parent.$submitted).toBeTruthy();
411411
});
412412

413+
it('should set $submitted equal to true on child forms when parent is submitted', function(){
414+
doc = jqLite(
415+
'<ng:form name="parent">' +
416+
'<ng:form name="child">' +
417+
'<input ng:model="modelA" name="inputA">' +
418+
'<input ng:model="modelB" name="inputB">' +
419+
'</ng:form>' +
420+
'</ng:form>');
421+
$compile(doc)(scope);
422+
423+
var parent = scope.parent,
424+
child = scope.child;
425+
426+
parent.$setSubmitted();
427+
expect(child.$submitted).toBeTruthy();
428+
});
413429

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

0 commit comments

Comments
 (0)