ChillAndCode
ChillAndCode

Reputation: 308

Block form validation of ReactiveForm when a field is hidden

I have a button in my form that is disabled until the form is valid. Everything works well when all my fields are visible. However, when a field is hidden by a condition (ngIf), the validation is done anyway. I would like to control this field only when it is visible.

My HTML

<form [formGroup]="form" (ngSubmit)="onRegister()">
    <div class="signup-form">
      <mat-radio-group>
        <mat-radio-button class="radio" value="candidat" (click)="resetCompany(false)" checked>Candidat
        </mat-radio-button>
        <br>
        <mat-radio-button class="radio" value="company" (click)="resetCompany(true)">Entreprise</mat-radio-button>
      </mat-radio-group>

      <mat-form-field>
        <input matInput placeholder="Nom" formControlName="lastName">
      </mat-form-field>
      <span *ngIf="lastName.invalid && lastName.touched" class="errorvalidation">
        Votre nom est requis.
      </span>

      <mat-form-field [ngStyle]="{'display':selectedCompany === true ? 'none' : 'inline-block' }">
        <input matInput placeholder="Prénom" formControlName="firstName">
      </mat-form-field>
      <span *ngIf="firstName.invalid && firstName.touched" class="errorvalidation">
        Votre prénom est requis.
      </span>

      <mat-form-field>
        <input matInput placeholder="Adresse e-mail" formControlName="email">
      </mat-form-field>
      <span *ngIf="email.invalid && email.touched" class="errorvalidation">
        L'email ne semble pas être au bon format.
      </span>

      <mat-form-field>
        <input matInput type="password" placeholder="Mot de passe" formControlName="password">
      </mat-form-field>
      <span *ngIf="password.invalid && password.touched" class="errorvalidation">
        Le mot de passe est requis.
      </span>

      <mat-form-field>
        <input matInput type="password" placeholder="Confirmez mot de passe" formControlName="confirmPassword">
      </mat-form-field>
      <div *ngIf="f.confirmPassword.errors" class="errorvalidation">
        <div *ngIf="f.confirmPassword.errors.required && confirmPassword.touched">Le champ est requis</div>
        <div *ngIf="f.confirmPassword.errors.mustMatch && confirmPassword.touched">Les mots de passes doivent être
          identiques</div>
      </div>

    </div>
    <div mat-dialog-actions align="end">
      <button mat-raised-button (click)="onNoClick()" color="warn">Annuler</button>
      <button class="btn" [disabled]="form.invalid" type="submit" mat-button [mat-dialog-close]>Je
        m'inscris</button>
    </div>
  </form>

My formGroup definition

ngOnInit() {
    this.form = this.formBuilder.group({
      firstName: ['', [Validators.required]],
      lastName: ['', [Validators.required]],
      email: ['', [Validators.required, Validators.email]],
      password: ['', Validators.required],
      confirmPassword: ['', Validators.required]
    }, {
      validator: MustMatch('password', 'confirmPassword')
    });
  }

How can I omit the validation of the field "firstName" when it is hidden ?

Thank you

Upvotes: 1

Views: 1458

Answers (1)

terahertz
terahertz

Reputation: 3511

I think you can do so by dynamically removing/adding the form control when the radio button is clicked. This way, the validation will not be executed unless the corresponding radio button is clicked.

component

  ngOnInit() {
    this.form = this.formBuilder.group({
      email: ['', [Validators.required, Validators.email]],
      password: ['', Validators.required],
      confirmPassword: ['', Validators.required]
    });

    this.resetCompany(false);
  }

  resetCompany(b){
    this.selectedCompany = b;
    if(!b){ //add control when "Candidat" selected
      this.form.addControl('firstName', new FormControl('', Validators.required))
      this.form.addControl('lastName', new FormControl('', Validators.required))
    }else{ //remove controls when "Entreprise" selected
      this.form.removeControl('firstName');
      this.form.removeControl('lastName');
    }
  }

template

<ng-container  *ngIf="selectedCompany === false && lastName != null">
<mat-form-field>
  <input matInput placeholder="Nom" formControlName="lastName">
</mat-form-field>
<span *ngIf="lastName.invalid && lastName.touched" class="errorvalidation">
  Votre nom est requis.
</span>
</ng-container>

<ng-container *ngIf="selectedCompany === false && firstName != null">
  <mat-form-field>
    <input matInput placeholder="Prénom" formControlName="firstName">
  </mat-form-field>
  <span *ngIf="firstName?.invalid && firstName?.touched" class="errorvalidation">
    Votre prénom est requis.
  </span>
</ng-container>

Example Stackblitz

Upvotes: 1

Related Questions