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

Closing MdDialog with md-dialog-close on button refreshes page #2599

Closed
JDillon522 opened this issue Jan 11, 2017 · 6 comments · Fixed by #2659
Closed

Closing MdDialog with md-dialog-close on button refreshes page #2599

JDillon522 opened this issue Jan 11, 2017 · 6 comments · Fixed by #2659
Assignees

Comments

@JDillon522
Copy link

Bug, feature request, or proposal:

Bug

What is the expected behavior?

I prepopulate a form inside of an MdDialog with data. I can click on a cancel button to close the dialog. That button is:

<button md-icon-button md-mini-fab color="warn" md-dialog-close>
   <md-icon  class="material-icons">cancel</md-icon>
</button>

The dialog then closes with no issues.

What is the current behavior?

When I click on the close button it refreshes the page with a query string containing the serialized data of the form.

What are the steps to reproduce?

Dialog component html:

<h3>Edit a User:</h3>
<form class="userForm">
  <md-input-container class="firstName">
    <input
      md-input
      placeholder="First Name"
      [(ngModel)]="first_name"
      name="first_name"
      type="text"
      required
      >
  </md-input-container>

  <md-input-container class="lastName">
    <input
      md-input
      placeholder="Last Name"
      [(ngModel)]="last_name"
      name="last_name"
      type="text"
      required
      >
  </md-input-container>

  <br>

  <md-input-container class="email">
    <input
      md-input
      placeholder="Email"
      [(ngModel)]="email"
      name="email"
      type="email"
      required
      >
  </md-input-container>

  <br>

  <div class="userActionButtonGroup">
    <button md-icon-button md-mini-fab color="accent" (click)="editUser()">
      <md-icon  class="material-icons">done</md-icon>
    </button>
    <button md-icon-button md-mini-fab color="warn" md-dialog-close>
      <md-icon  class="material-icons">cancel</md-icon>
    </button>
  </div>

</form>

typescript:

import { Component, OnInit } from '@angular/core';
import { MdDialog, MdDialogRef } from '@angular/material';
import { User } from '../../../services/user';
import { UsersService } from '../../../services/users.service';

@Component({
  selector: 'app-edit-user',
  templateUrl: './edit-user.component.html',
  styleUrls: ['./edit-user.component.scss'],
  providers: [ UsersService ]
})
export class EditUserComponent implements OnInit {
  private errorMessage: string;
  public id: number;
  public first_name: string;
  public last_name: string;
  public email: string

  constructor(
    private usersService: UsersService,
    private dialogRef: MdDialogRef<EditUserComponent>
  ) { }

  ngOnInit() {
  }

  editUser() {
    let thisUser = {
      first_name: this.first_name,
      last_name: this.last_name,
      email: this.email
    };

    this.usersService.editUser(this.id, thisUser)
                      .subscribe(
                        (user) => {
                          this.dialogRef.close('updateUsers');
                        }, (error) => {
                          this.errorMessage = <any>error;
                        });
  }
}

#### Which versions of Angular, Material, OS, browsers are affected?

angular-cli: 1.0.0-beta.24
node: 6.9.3
os: darwin x64
@angular/common: 2.4.2
@angular/compiler: 2.4.2
@angular/core: 2.4.2
@angular/forms: 2.4.2
@angular/http: 2.4.2
@angular/material: 2.0.0-beta.1
@angular/platform-browser: 2.4.2
@angular/platform-browser-dynamic: 2.4.2
@angular/router: 3.4.2
@angular/compiler-cli: 2.4.2


#### Is there anything else we should know?
I have the exact same functionality on two other dialogs except that those two are not prepopulated with forms.
@crisbeto
Copy link
Member

crisbeto commented Jan 11, 2017

It's because the button ends up submitting the form by default. You should be able to work around it by setting type="button" on it. @jelbourn do you think that this is something that we should handle automatically?

@JDillon522
Copy link
Author

That fixed it. The odd part is that it only occurred on that dialog when its form had pre-populated data. The other dialogs I have with forms dont do that.

@jelbourn
Copy link
Member

I think it would be reasonable to have the dialog-close-button always set type="button"

@crisbeto crisbeto self-assigned this Jan 11, 2017
@karolmie1
Copy link

karolmie1 commented Jan 12, 2017

@crisbeto @jelbourn This does not work! Remember, that you can close form also with pushing enter button, and that would fire submit event (and thus cause reload) even if you've specified the button to be type="button" (learned it the hard way). The only way is to do something like this:

<form role="form" (submit)="close($event, filterValue.value)">
  <input #filterValue/>
  <button type="submit">Szukaj</button>
</form>

private close(event: Event, value: string) {
  event.preventDefault();
  this.dialogRef.close(value);
}

Thats not really a bug with material, more like inconvenience within angular.
Should be described in documentation though.

@jelbourn
Copy link
Member

Pressing enter will only submit the form if there is a submit button inside of the form.

crisbeto added a commit to crisbeto/material2 that referenced this issue Jan 15, 2017
Prevents the `md-dialog-close` directive from submitting any forms that it is inside of.

Fixes angular#2599.
mmalerba pushed a commit that referenced this issue Jan 18, 2017
Prevents the `md-dialog-close` directive from submitting any forms that it is inside of.

Fixes #2599.
kara pushed a commit to kara/material2 that referenced this issue Jan 20, 2017
…2659)

Prevents the `md-dialog-close` directive from submitting any forms that it is inside of.

Fixes angular#2599.
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants