37

Angular - Submit a form programmatically.

I have a form group on the HTML and I want the component to submit the form's action with an email field in a post method. Instead of using a normal submit button.

Below's testMethod gets called from another button. In this method, I want to post the testForm. It has to be posted old school way as it needs an action.

This is my HTML:

<form
  [formGroup]="testGroup"
  [action]='actionLink'
  method='POST'
  #testForm>
   <input name='Email' type='hidden' [value]='currentUserEmail'>
</form>

This is my Component TS file attempt:

  @ViewChild('testForm') testFormElement;

  public currentUserEmail: string = '';
  public testGroup = this.formBuilder.group({
    Email: ''
  });


  public testMethod(): void {

      // Below: This currently doesnt seem to do anything.
      this.testFormElement.ngSubmit.emit();
  }
5
  • can you please tell us what is the reason of the error that we see in the console ? and what happened exactly when you click and the button ( when you call testMethod() ) ? Thanks Commented Jan 3, 2018 at 15:04
  • It seems like the this.testFormEl.nativeElement.submit() is doing nothing. I cant see the post in the network traffic Commented Jan 3, 2018 at 15:42
  • @MohamedAliRACHID I've added my current attempt above Commented Jan 3, 2018 at 15:57
  • @AngularM have you solved this? Commented Mar 8, 2018 at 10:04
  • this solution worked for me. Commented Mar 26, 2019 at 10:55

8 Answers 8

20

You can use ngNoForm with your form to remove ngForm handling and to add plain javascript handler.

you can use your code as follows:

Html file.

<form ngNoForm
  [formGroup]="testGroup"
  [action]='actionLink'
  method='POST'
  #testForm>
    <input name='Email' type='hidden' [value]='currentUserEmail'>
</form>

Ts File.

@ViewChild('testForm') testFormElement;

public currentUserEmail: string = '';
public testGroup = this.formBuilder.group({
  Email: ''
});

constructor(formBuilder: FormBuilder) {
}    

public testMethod(): void {
  this.testFormElement.nativeElement.submit();
}
Sign up to request clarification or add additional context in comments.

Comments

14

I think you should get ngForm in your code. So, rewrite your code as follows:

<form
[formGroup]="testGroup"
[action]='actionLink'
method='POST'
#testForm="ngForm" (ngSubmit)="testForm.form.valid ? yourSaveMethod() :showValidatinErrors()">
  <input name='Email' type='hidden' [value]='currentUserEmail'>
</form>

and in your ts file:

@ViewChild('testForm') testFormElement: NgForm;

public testMethod(): void {
  // Below: This works for me.
  this.testFormElement.ngSubmit.emit();
}

public yourSaveMethod(): void {
  // post your model here.
}

1 Comment

If you want to really simulate submit you should also emit your values this.testFormElement.ngSubmit.emit(this.testGroup.values);
3

I have found a good solution on github

it will setup submitted as true

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

Comments

0
import { Component, ViewChild } from '@angular/core';

@Component({
    template: `
        <form #testForm>
            <input name='Email' type='hidden'>
        </form>
    `
})
class MyComponent {
    @ViewChild('testForm') testFormEl;

    testMethod() {
        this.testFormEl.nativeElement.submit()
    }
}

2 Comments

I've added the error I get above in my question. Your solution didnt work
I have tried this code but responding an error that nativeElement is undefined. core.js:14597 ERROR TypeError: Cannot read property 'nativeElement' of undefined
0

Regarding "Angular - Submit a form programmatically" Here is code where a form submits itself. It works for me. Hope this helps.

Component HTML:

<form #myForm ngNoForm name="myForm" [action]="pdfServletUrl" target="_blank" method="POST">
  <button type="submit" [hidden]="'true'"></button>
</form>

Component TypeScript:

@ViewChild('myForm') myForm : ElementRef;

ngAfterViewInit() {
  this.myForm.nativeElement.submit();
}

Comments

0

I found the following method to be the best. I believe this is the intended method of achieving this.

<form #tf (ngSubmit)="saveSomething()" [formGroup]="somethingForm" (keydown.enter)="$event.preventDefault()">
    <input type="text" formControlName="something" (input)="tf.requestSubmit()">
</form>

Comments

0

This worked for me :

@ViewChild('testForm') testFormElement : NgForm;

public testMethod(): void {
   this.testFormElement.ngSubmit.emit();
   (this.testFormElement as any).submitted = true;
}

I found it from github.

Comments

-1

If u want to make form.submitted = true u need to use event

submitClicked(form: NgForm) {
    form.onSubmit(new Event('submit'));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.