v4: Unexpected reactive form submit behavior
See original GitHub issueBug Report
Ionic:
ionic (Ionic CLI) : 4.0.0-rc.11 (C:\Users\matth\AppData\Roaming\npm\node_modules\ionic)
Ionic Framework : @ionic/angular 4.0.0-alpha.9
@angular-devkit/core : 0.7.0-rc.1
@angular-devkit/schematics : 0.7.0-rc.1
@angular/cli : 6.0.8
@ionic/ng-toolkit : 1.0.0-rc.10
@ionic/schematics-angular : 1.0.0-rc.10
System:
NodeJS : v8.9.4 (C:\Program Files\nodejs\node.exe)
npm : 5.6.0
OS : Windows 10
Describe the Bug Unclear if these are Angular or Ionic issues. Works in Ionic 3.
- Enter key does not submit Angular reactive form.
- Pre-filled Angular reactive form is invalid when submitted (even though form fields valid).
Steps to Reproduce Steps to reproduce the behavior:
- Create a test page with a simple Angular reactive form. Code below.
- Click enter on keyboard. Form is not submitted.
- Click submit button.
- console logs “Form is invalid”
Related Code
test.page.html
<ion-header>
<ion-toolbar>
<ion-title>test</ion-title>
</ion-toolbar>
</ion-header>
<ion-content padding>
<form [formGroup]="form" #ngForm="ngForm">
<ion-item>
<ion-label position="floating">Email</ion-label>
<ion-input value="test@test.com" formControlName="email" type="email"></ion-input>
</ion-item>
<ion-item *ngIf="form.controls.email.invalid && (form.controls.email.dirty || form.controls.email.touched || ngForm.submitted)" class="validation-message">
<p>Enter a valid email.</p>
</ion-item>
<ion-button type="submit" (click)="submit($event)">Submit</ion-button>
</form>
</ion-content>
test.page.ts
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
@Component({
selector: 'test',
templateUrl: './test.page.html',
styleUrls: ['./test.page.scss'],
})
export class TestPage implements OnInit {
private form: FormGroup;
constructor(
public formBuilder: FormBuilder
) { }
ngOnInit() {
this.form = this.formBuilder.group({
email: ['', Validators.compose([Validators.email, Validators.required])],
});
}
private submit(event) {
if (this.form.valid) {
console.log('Form is valid.')
} else {
console.log('Form is invalid.')
}
}
}
test.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';
import { Routes, RouterModule } from '@angular/router';
import { IonicModule } from '@ionic/angular';
import { TestPage } from './test.page';
const routes: Routes = [
{
path: '',
component: TestPage
}
];
@NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
RouterModule.forChild(routes),
ReactiveFormsModule
],
declarations: [TestPage]
})
export class TestPageModule { }
Expected Behavior
- Enter key submits form.
- Pre-filled form with valid values results in this.form.valid = true
Issue Analytics
- State:
- Created 5 years ago
- Reactions:5
- Comments:5
Top Results From Across the Web
v4: Unexpected reactive form submit behavior · Issue #14786
Works in Ionic 3. Enter key does not submit Angular reactive form. Pre-filled Angular reactive form is invalid when submitted (even though form ......
Read more >Unexpected behaviour between Angular custom validator for ...
I am working on a formBuilder of a website register form. import { Component, OnInit, Injectable } from '@angular/core'; ...
Read more >Angular Reactive Forms: Tips and Tricks | by Netanel Basal
The way we get around this is by using the updateOn property. We can tell Angular that we only want to run the...
Read more >Conditional form validation with Angular Reactive Forms
In this tutorial you will learn how to use Angular Reactive Forms to implement conditional field validation in an Ionic/Angular application.
Read more >Reactive forms - Angular
The FormGroup directive listens for the submit event emitted by the form element and emits an ngSubmit event that you can bind to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I’ve upgraded to the latest Ionic 4 release and created an example app with one test page and one simple angular reactive form: https://github.com/Cymetrik/ionic4-form.
There’s now an additional issue where clicking the submit button refreshes the whole page.
@matthew-valenti If it helps, here’s a workaround to submit the form, till the main issue of page reload is fixed:
Here is the modified submit method in TS: