简体   繁体   English

单击隐藏和显示的切换时,表单字段将生效

[英]Form fields are validating on click of toggle for hide and show

I'm showing the part of the form to hide and show when clicking on the button. 我正在显示单击按钮时要隐藏和显示的表单部分。 the form fields are becoming invalid before on click of the button itself which is annoying for the end user. 表单字段在单击按钮本身之前变得无效,这对于最终用户来说很烦人。

app.component.html: 

      <div class="row">
          <h3>
            <button mat-button (click)="toggle()">
              Change password
            </button>
          </h3>
        </div>
        <div *ngIf="hidden">
          <div fxLayout="row" fxLayoutAlign="center stretch">
            <div fxLayout="column" fxFlex="80">
              <label> Current password :</label>
              <mat-form-field appearance="outline">
                <input formControlName="oldPassword" type="password" matInput 
                 placeholder="Current password">
              </mat-form-field>
              <mat-error
                *ngIf="profileForm.get('oldPassword').hasError('required') && 
                 profileForm.get('oldPassword').touched">
                Password is required
              </mat-error>
            </div>
          </div>
       </div>

app.component.ts:

 toggle(){
    this.hidden = !this.hidden; 
  }

Thanks for viewing my issue! 感谢您查看我的问题! Actually, I was using the same formGroup for the whole form, then I separated it and added another formGroup I mean like another form for the part of the code and validations in form builder this helped me to resolve. 实际上,我对整个表单使用了相同的formGroup,然后将其分离并添加了另一个formGroup,我的意思是像另一表单一样用于代码部分和表单生成器中的验证,这有助于我解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM