Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

request| feat(form): Ability to programmatically submit an AbstractControl, NgForm or a FormGroupDirective #20562

Open
lemoinem opened this issue Nov 21, 2017 · 6 comments
Labels
area: forms feature: under consideration Feature request for which voting has completed and the request is now under consideration feature Issue that requests a new feature freq2: medium state: Needs Design
Milestone

Comments

@lemoinem
Copy link
Contributor

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

In standard HTML/TypeScript, without angular, it's possible to get a HTMLFormElement and call either .sumbit() or .reset() on it to submit it programmatically. If we want to do this in an Angular2 Component, we will need to either:

  • have a @ViewChild('myForm') formElementRef, casting the formElementRef.nativeElement to HtmlFormElement and then call .submit() on it
  • have a hidden input type="sumbit" (or similar), again do a @ViewChild on it and trigger it programmatically.

Note that in the case of .reset() this is very well supported and documented.

Expected behavior

To be able to trigger the .submit() operation directly from an FormGroup/FormArray or even an NgForm would be really helpful.
There are cases where, for practical UI/UX reasons the submit button cannot be in the <form> element itself. Although this is far from ideal, it's still something that can be and is done.

Minimal reproduction of the problem with instructions

`<button #submit>Submit!

[...]`

Then try to submit the form without using an ElementRef or a hidden submit inside the <form>.

What is the motivation / use case for changing the behavior?

Some UIs will require us to have the submit element outside of the <form>, or to submit a form relying on different mechanism/conditions (e.g., time triggered for a game-like UI).

Environment


Angular version: 5.1.0-beta.1

@lemoinem lemoinem changed the title feat(form): Ability to programmatically submit an AbstractControl, NgForm or a FormGroupDirective request| feat(form): Ability to programmatically submit an AbstractControl, NgForm or a FormGroupDirective Nov 21, 2017
@kara kara added freq2: medium feature Issue that requests a new feature state: Needs Design labels Nov 21, 2017
@ngbot ngbot bot added this to the Backlog milestone Jan 23, 2018
@msc117
Copy link

msc117 commented Mar 6, 2018

The only way I'm able to do this currently is to use this code

form.ngSubmit.emit();
(form as any).submitted = true;

You need to cast as any because typescript will complain submitted is a readonly property

@Lonli-Lokli
Copy link

Out approach now is:
html:
<form [formGroup]="form" #myForm="ngForm" novalidate class="create-form">

ts:

@ViewChild('myForm') ngForm: NgForm;

...
public onSubmit() {
   this.ngForm.onSubmit(undefined); // it will setup submitted as true

@nischi
Copy link

nischi commented Jun 29, 2020

The only way I'm able to do this currently is to use this code

form.ngSubmit.emit();
(form as any).submitted = true;

You need to cast as any because typescript will complain submitted is a readonly property

I do a override class which avoid a any cast.

// separate class
export class FormGroupDirectiveOverride extends FormGroupDirective {
  // Make submitted writeable because reactive forms can be submitted programmatically
  // https://github.com/angular/angular/issues/20562
  submitted: boolean;
}

// component
@ViewChild(FormGroupDirective) formGroupDirective: FormGroupDirectiveOverride;
this.formGroupDirective.submitted = true;

@angular-robot angular-robot bot added the feature: under consideration Feature request for which voting has completed and the request is now under consideration label Jun 4, 2021
@DmitryEfimenko
Copy link

why is the submitted property readonly? Maybe someone from Angular team can shine some light?

@JeanMeche
Copy link
Member

@DmitryEfimenko submitted is meant to be set by the submit event of the host of the FormGroupDirective.

@JeanMeche
Copy link
Member

JeanMeche commented May 20, 2024

Btw this exemple mentionned by OP, a button outside the form can be handled with bare HTML : reference the form to the submit button.

------v
<form id="myform" [formGroup]="myFg">
    ....
</form>

<input type="submit" form="myform" value="Update"/>
---------------------^

That being said, I'm not sure we should implement this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: forms feature: under consideration Feature request for which voting has completed and the request is now under consideration feature Issue that requests a new feature freq2: medium state: Needs Design
Projects
None yet
Development

No branches or pull requests

8 participants