Click on a button in a MatFormFieldControl trigger the Submit action on Form in Angular 4
Asked Answered
T

2

6

I have developed a custom Component (app-mat-avatar) to use as an avatar choose (See picture). It consists of one big div with a picture and 2 buttons. One to edit, the other to erase. I built it following Angular guide at Creating a custom form field control enter image description here

This is the form where I use it:

<form name="detailsForm" [formGroup]="detailsForm" (submit)="submitDetails($event)">
    <app-mat-avatar [size]="150" formControlName="photoURL"></app-mat-avatar>
    <input matInput type="email" placeholder="Email" formControlNamesss="email" disabled value="{{detailsForm.get('email').value}}">
.....

    <div flex layout="row" layout-align="end center" class="fullWidth pad-right-sm">
        <button mat-button mat-raised (click)="resetDetails()" class="push-right-xs" [disabled]="busy">Reset</button>
        <button type="submit" mat-button mat-raised-button color="primary" [disabled]="detailsForm.invalid || busy">Save</button>
    </div>
</form>

And this is the code supporting the page Component where the form is located:

@Component({
  selector: 'app-my-info',
  templateUrl: './my-info.component.html',
  styleUrls: ['./my-info.component.sass']
})
export class MyInfoComponent implements OnInit, OnDestroy {

  detailsForm = new FormGroup({
    uid: new FormControl(''),
    photoURL: new FormControl(''),
    firstName: new FormControl('', [Validators.required, Validators.minLength(2)]),
    lastName: new FormControl('', [Validators.required, Validators.minLength(2)]),
    sex: new FormControl('', [Validators.required]),
    email: new FormControl('', [Validators.required, Validators.email]),
    licenseLocal: new FormControl(''),
    licenseNational: new FormControl(''),
    cellPhone: new FormControl(''),
  });

........

  ngOnInit() {
    fetchUserFromDatabase.subscribe( me => {
      if (!me) {return; }
      this._currentUser = me;      
      this.resetDetails();

    }));
  }

  resetDetails() {
    const user = {
      email : this.currentUserCopy.email,
      photoURL: this.currentUserCopy.photoURL,
      firstName: this.currentUserCopy.firstName,
      lastName: this.currentUserCopy.lastName,
      licenseLocal: this.currentUserCopy.licenseLocal || '',
      licenseNational: this.currentUserCopy.licenseNational || '',
      sex: this.currentUserCopy.sex,
      cellPhone: this.currentUserCopy.cellPhone || ''
    };
    this.detailsForm.patchValue( user );
  }


  submitDetails(event) {
    // console.log(event);
    this._currentUser.createFromFirebaseData(this.detailsForm.value);
    this._db.UpdateUser(this._currentUser)
    .then( result => {
      this.busy = false;
      this._popUp.showInfo('Perfil guardado');
    })
    .catch( error => {
      this.busy = false;
      this._popUp.showError(error);
    });
  }
}

Everything works well except that when I click on one of the button of the app-mat-avatar component, the submitDetails method is triggered.

I tried to add event.stopPropagation(); as the first line of the method the button click calls, but it has no effect.

Any idea why this could be happening?

Tamarau answered 16/11, 2017 at 22:7 Comment(0)
A
12

Add type="button" to the reset button in your template to prevent the submission

Alleris answered 16/11, 2017 at 23:49 Comment(2)
That actually didn't work, BUT... it was a great hint!! I put type="button" on both buttons of the custom component (app-mat-avata) and this time, it worked!! Thanks a lot @Alleris ! By the way, would you care to elaborate a little on the reason why it worked ?Tamarau
I dont have the exact answer to that, but had the problem many times, it seems that when you dont specify the type it thinks that is a submit buttonAlleris
P
3

Set type="reset" like below,

<button mat-button mat-raised type="reset" (click)="resetDetails()" class="push-right-xs" [disabled]="busy">Reset</button>
Partridge answered 12/3, 2019 at 15:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.