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

v4: Unexpected reactive form submit behavior #14786

Closed
matthew-valenti opened this issue Jul 17, 2018 · 5 comments
Closed

v4: Unexpected reactive form submit behavior #14786

matthew-valenti opened this issue Jul 17, 2018 · 5 comments
Assignees

Comments

@matthew-valenti
Copy link

Bug 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.

  1. Enter key does not submit Angular reactive form.
  2. Pre-filled Angular reactive form is invalid when submitted (even though form fields valid).

Steps to Reproduce
Steps to reproduce the behavior:

  1. Create a test page with a simple Angular reactive form. Code below.
  2. Click enter on keyboard. Form is not submitted.
  3. Click submit button.
  4. 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

  1. Enter key submits form.
  2. Pre-filled form with valid values results in this.form.valid = true
@ionitron-bot ionitron-bot bot added the triage label Jul 17, 2018
@matthew-valenti
Copy link
Author

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.

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.11
   @angular-devkit/core       : 0.7.0-rc.2
   @angular-devkit/schematics : 0.7.0-rc.2
   @angular/cli               : 6.0.8
   @ionic/ng-toolkit          : 1.0.0-rc.11
   @ionic/schematics-angular  : 1.0.0-rc.11

System:

   NodeJS : v8.9.4 (C:\Program Files\nodejs\node.exe)
   npm    : 5.6.0
   OS     : Windows 10

@devner007
Copy link

@matthew-valenti
I just tested your latest code and I do confirm that I am seeing the same behavior as you are. It reloads the whole page! I did not debug this yet, but tried to recreate the same issue in a brand new project. I am shocked to find out that in the new project, I can't even work with the Reactive forms, even though I have imported FormsModule & ReactiveFormsModule in the app.module.ts file. Following are some screenshots of the issue:

image

image

image

image

Any help in solving the issue is greatly appreciated.

@devner007
Copy link

@matthew-valenti
If it helps, here's a workaround to submit the form, till the main issue of page reload is fixed:

<form [formGroup]="form">
    <ion-item>
      <ion-label position="floating">Email</ion-label>
      <ion-input formControlName="email" type="email"></ion-input>
    </ion-item>
    <ion-item *ngIf="form.controls['email'].invalid &&
                    (form.controls['email'].dirty ||
                      form.controls['email'].touched )" class="validation-message">
      <p>Enter a valid email.</p>
    </ion-item>
    <div class="button-container">
      <ion-button type="button" (click)="submit(form.value)">Submit</ion-button>
    </div>
  </form>

Here is the modified submit method in TS:

submit(formObject) {
        console.warn(formObject);
        if (this.form.valid) {
            console.log('Form is valid.');
        } else {
            console.log('Form is invalid.');
        }
    }

@declanelcocks
Copy link

Can confirm this issue is there also. This issue blocks me from updating to v4 really since, without functioning forms, most existing apps are going to be a pain to migrate over.

@ionitron-bot
Copy link

ionitron-bot bot commented Sep 1, 2018

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Sep 1, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants