ngSubmit not working
Asked Answered
M

7

18

I have an angular 4 form where I am trying to submit data

HTML Code

<form class="form-horizontal"   [formGroup]="signupForm" 
(ngSubmit)="onFormSubmit()" >
<fieldset>
  <legend>Scheduler</legend>
    <!--- Gender Block -->
    <div class="form-group">
    <label for="scheduleJob">Schedule Job</label>
    <input type="text" formControlName = "schedulejob"
      id="scheduleJob"
      placeholder="Schedule Job">

Button code

 <div class="form-group">
        <button type="reset" class="btn btn-default">Cancel</button>           
        <button type="submit"  class="btn btn-primary">Submit</button>          

    </div>

Scheduler.TS file

import { Component, OnInit } from '@angular/core';
import { Scheduler } from 'rxjs/Scheduler';
/* Import FormControl first */
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-scheduler',
  templateUrl: './scheduler.component.html',
  styleUrls: ['./scheduler.component.css']
})
export class SchedulerComponent implements OnInit {

  //Gender list for the select control element
 private scheduleTypeList: string[];
 //Property for the user
 private scheduler:Scheduler;

 private signupForm: FormGroup;

 constructor(private fb:FormBuilder) { } 

  ngOnInit() {   

    this.scheduleTypeList =  ['Type 1', 'Type 2', 'Type 3'];
    this.signupForm  = this.fb.group({
      schedulejob:  ['', Validators.required] ,               
      frequency: ['', Validators.required],
      days: ['', Validators.required],
      zone: ['', Validators.required],
      schedulerjobtype:['', Validators.required]
  })  
  } 
  public onFormSubmit() {
    this.scheduler = this.signupForm.value;
    if(this.signupForm.valid) {
        this.scheduler = this.signupForm.value;
        console.log(this.scheduler);
      // alert(this.scheduler);
        /* Any API call logic via services goes here */
    }
    //alert(this.scheduler);
    console.log(this.scheduler);
}
}

Why is the execution not passed to onFormSubmit on submit click and alert or console.log not printing values?

Melchizedek answered 1/6, 2018 at 6:9 Comment(4)
Is your button inside the form?Geoid
Can you share the module.ts which declares SchedulerComponent?Hum
thanks @chrilewoodz thats the issue, please post as answer i will accept itMelchizedek
@Geoid please post it as answer i will accept itMelchizedek
G
54

Like I said in the comment, if your button is not inside your form it will not submit it.

So change to:

<form>
  ...
  <button type="submit">Submit</button>
</form>

It is possible however to have it outside if you do it a bit differently, as described in this question.

Geoid answered 1/6, 2018 at 7:18 Comment(3)
That's 30 minutes i am not going to get back - thanks!!Kennith
I had button type="button" instead of button type="submit".Panto
Place the button inside the form as above AND button type must be submit.Bizet
T
4

I was having the same issue because I was not using a <form> tag to wrap my form.

Taal answered 29/1, 2020 at 2:36 Comment(0)
R
3

you need to put the button code inside the form like below

<form class="form-horizontal"  
 [formGroup]="signupForm" 
(ngSubmit)="onFormSubmit()" >
<fieldset>
<legend>Scheduler</legend>
<!--- Gender Block -->
<div class="form-group">
<label for="scheduleJob">Schedule Job</label>
<input type="text" formControlName = "schedulejob"
  id="scheduleJob"
  placeholder="Schedule Job">

  <!-- Button Code Here -->

    <div class="form-group">
    <button type="reset" class="btn btn-default">Cancel</button>           
    <button type="submit"  class="btn btn-primary">Submit</button>          

  </div>
   <!--  Form ends here -->
 </form>
Rorry answered 1/6, 2018 at 6:30 Comment(0)
C
3

When it happened to me, I checked literally all the ways possible . Then finally I found the solution is that I did not include the FormsModule in app.module file.

Commentary answered 18/8, 2023 at 9:38 Comment(0)
S
2

a small trick you can do is to place an auxiliary hidden button inside the form and let the main button programmatically click the auxiliary button:

<form [formGroup]="form" (ngSubmit)="submit()">
  ...
  <button type="submit" hidden #btn></button>
</form>
...
<button (click)="btn.click()">Submit</button>
Saddlery answered 9/9, 2020 at 21:25 Comment(2)
The solution in the link of @Geoid answer is much better. Use the form attribute of the button.Cleave
Sure, but beware that the form attribute is not supported by IE.Saddlery
H
0

You can also use the form attribute in the submit button. This attribute uses the form's id to refer to it:

<form id="formID">
      ...
      <button type="submit" form="formID">Submit</button>
</form>
Hunley answered 15/6, 2021 at 16:20 Comment(0)
J
0

I was encoutering same issue. After struggling a lot, I realized that I have called event.stopProgogation() and event.preventDefault() in my button's click method. These method calls was preventing form submission when user clicked the button. Solution of my issue was only removal of these method calls.

Joacima answered 23/5, 2023 at 16:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.