简体   繁体   English

angular 5,reactive form-重置所需的表单控件不会重置输入下的所需错误

[英]angular 5, reactive form- resetting a required form control does not reset the required error under the input

In the template I have a form that is opened by pressing on a button-在模板中,我有一个通过按下按钮打开的表单 -

     <form [formGroup]="person"
          (ngSubmit)="onSubmitNewPerson()"
          #formDirective="ngForm">

            <mat-form-field>
                <input matInput formControlName="firstName" required>
            </mat-form-field>

            <mat-form-field>
                <input matInput formControlName="lastName" #last required>
            </mat-form-field>
</form>

In the component I have this code-在组件中,我有这个代码-

  @ViewChild('formDirective') formDirective: FormGroupDirective;

 this.person = this.formBuilder.group({
            lastName: ['', Validators.required],
            firstName: ['', Validators.required]
        });

After closing the button I run this function-关闭按钮后,我运行此功能-

   this.formDirective.resetForm();//hack that I saw in some question
    this.person.reset();

but after openning again the form, I immediatly see the red error under the input.但是再次打开表单后,我立即看到输入下的红色错误。

I also tried this-我也试过这个-

    this.person.get('firstName').markAsUntouched();
   this.person.get('lastName').markAsUntouched();
     this.person.get('firstName').markAsPristine();
    this.person.get('lastName').markAsPristine();

But this does not work also.但这也不起作用。

Any idea how to fix it?知道如何修复它吗?

I used the following once when I wanted to reset the validators:当我想重置验证器时,我使用了以下一次:

    this.person.get('firstName').clearValidators();
    this.person.get('firstName').updateValueAndValidity();

Simply remove required from your html template and if you want to display error message on focus out than try this to show different error message.只需从您的 html 模板中删除 required ,如果您想在焦点上显示错误消息,而不是尝试显示不同的错误消息。

this is html template:这是 html 模板:

<div class="form-group">
        <label for="name" class="form-control-label">
        *Amount:
        </label>
        <input type="number" name="amount" class="form-control"  [formControl]="form.controls['amount']">
        <div>
        <small *ngIf="form.controls['amount'].hasError('required') && form.controls['amount'].touched" class="text-danger">
           Please enter an amount.
        </small>
        <small  *ngIf="form.controls['amount'].hasError('min') && form.controls['amount'].touched" class="text-danger">
             Your amount must be at least 0.01.
        </small>
        <small  *ngIf="form.controls['amount'].hasError('max') && form.controls['amount'].touched" class="text-danger">
                Your amount cannot exceed 999999.99.
        </small>
        </div>

This is component.ts这是 component.ts

 import { FormGroup, FormBuilder, Validators} from '@angular/forms';

constructor(private fb: FormBuilder) {  }

this.form = this.fb.group({
    amount: [null, Validators.compose([Validators.required, Validators.min(0.01), Validators.max(999999.99)])],
  });

Although this question is slightly old, This answer may help people who have this issue.虽然这个问题有点老,但这个答案可能会帮助有这个问题的人。

If this problem occurs in the Angular material mat-stepper , you can see the solution offered for this question .如果这个问题出现在 Angular material mat-stepper ,您可以查看针对此问题提供的解决方案。

(That why some respondents were unable to reproduce the problem) (这就是为什么一些受访者无法重现问题的原因)

暂无
暂无

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

相关问题 Angular 5反应性表单-通过html更新formControl不会更新值,只有控件 - Angular 5 reactive form- updating formControl through the html does not update the value, only the control Angular 反应式表单 - 禁用所需的表单控件使表单有效,即使没有给出值 - Angular reactive form - Disabling a required form control makes the form valid even if no value is given 需要 angular 反应式表单切换验证吗? - angular reactive form toggle validation required? Angular Reactive Form中的FormArray不会在表单提交时重置 - FormArray in Angular Reactive Form not resetting on form submit 自定义验证器在以角度 2 重置反应形式时抛出错误 - Custom Validator throw error upon resetting reactive form in angular 2 在不触发错误渲染的情况下重置 Angular 反应式表单? - Resetting an Angular Reactive Form without triggering error rendering? 禁用所需的输入时,角度形式无效 - Angular form is invalid when a required input is disabled Angular 6 只需要许多字段中的一个字段 Reactive Form - Angular 6 Required only one field from many fields Reactive Form Angular 5-反应形式-是否可以使用变量修补值? - Angular 5 - reactive form- is there a way to patch value using a variable? Angular Form Validation:$ error.required甚至在使用自定义输入指令的ng-required = false时设置 - Angular Form Validation: $error.required set even when ng-required=false with custom input directive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM