I wanna send a form inside a form, but when I submit the form-inside-the-form, all the forms are submitted now. Is it even possible to do this?
<form name="add_foobar_form" novalidate ng-submit="addFoobar(foobar)">
<form name="send_contact_form" novalidate ng-submit="sendContact(hello)">
<input type="text" ng-model="hello.bye">
<input type="submit" value="sendmall">
</form>
<input type="text" ng-model="foobar.go">
<input type="submit" value="sendall">
</form>
What is a Nested Form? In simple words, nested forms are forms within a form. Using nested forms, we can create an array of objects within a single field and we can have an array of these fields. Hence, the nested form helps us manage large form groups and divides it into small groups.
You can use plenty of SUBMIT buttons inside a single form, but you can't manage a nested form appropriately.
These are the 3 steps required to create Nested Reactive Form, Step 1: create a new application in angular using the command. “ng new new-app.” Step 2: Import the forms module and reactive forms module in the app.
Every form must be enclosed within a FORM element. There can be several forms in a single document, but the FORM element can't be nested.
Can you have nested forms? Simply no, Nested form won't work.
Docs Says
In Angular, forms can be nested. This means that the outer form is valid when all of the child forms are valid as well. However, browsers do not allow nesting of elements, so Angular provides the ngForm directive which behaves identically to but can be nested. This allows you to have nested forms, which is very useful when using Angular validation directives in forms that are dynamically generated using the ngRepeat directive
But unfortunately you could not use the ng-submit
form, that will call the parent ng-submit
method on click of any inner form.
Example Plunkr
Workaround for this would be don't use ng-submit
directive for the inner nested from. You should use ng-click
on button and change type of button to button
from submit
.
Markup
<form name="add_foobar_form" novalidate ng-submit="addFoobar('foobar')">
<ng-form name="send_contact_form" novalidate>
<input type="text" ng-model="hello.bye">
<input type="button" value="sendmall" ng-click="sendContact('hello')">
</ng-form>
<input type="text" ng-model="foobar.go">
<input type="submit" value="sendall">
</form>
Workaround Plunkr
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With