隐藏字段时对ReactiveForm进行块形式验证

时间:2019-11-20 21:24:27

标签: angular angular-reactive-forms

我的表单中有一个按钮,该按钮在表单有效之前将被禁用。当我所有的字段都可见时,一切正常。但是,当某个字段被条件(ngIf)隐藏时,无论如何都要进行验证。我只想在可见时控制此字段。

我的HTML

(Semigroup (t a))

我的表单组定义

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

隐藏字段“ firstName”时如何取消验证?

谢谢

1 个答案:

答案 0 :(得分:0)

我认为您可以通过单击单选按钮时动态删除/添加表单控件来实现。这样,除非单击相应的单选按钮,否则将不会执行验证。

组件

  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');
    }
  }

模板

<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 ?