来自错误验证的 Angular 11 反应不起作用

马西布

我是 Angular 的新手。我正在尝试通过实践来学习它。我尝试了如下反应式表单验证。它不起作用。我看不出有什么问题:-

TS:-

export class LoanTypesComponent implements OnInit {

addLoanTypesForm!: FormGroup;

constructor(private fb: FormBuilder) { }

ngOnInit(): void {
  this.addLoanTypesForm =   this.fb.group({
      'loanName' : new FormControl('', [Validators.required,
      Validators.maxLength(10), Validators.minLength(4)]),
      'loanType' : new FormControl('', Validators.required),
      'terms' : new FormControl(),
      'loanDescription' : new FormControl()

  });
}
}

HTML:-

  <form [formGroup]="addLoanTypesForm">

<div class="form-group">
  <input type="text" formControlName="loanName"> <br/>
  <div class="text-danger" *ngIf="addLoanTypesForm.get('loanType')?.hasError('minlength') && addLoanTypesForm.get('loanType')?.touched  ">This field minLength is 4 </div>
  <div class="text-danger" *ngIf="addLoanTypesForm.get('loanType')?.hasError('maxlength') && addLoanTypesForm.get('loanType')?.touched  ">This field max length is 10 </div>
  <div class="text-danger" *ngIf="addLoanTypesForm.get('loanType')?.hasError('required') && addLoanTypesForm.get('loanType')?.touched  ">This field is required </div>
</div>
<input type="text" formControlName="loanType"> <br/>
<div *ngIf="addLoanTypesForm.get('loanType')?.hasError('required') && addLoanTypesForm.get('loanType')?.touched  ">This field is required </div>
<input type="checkbox" formControlName="terms"> Accept Terms! <br/>
<textarea fromControlName="loanDescription"></textarea><br/>
<button [disabled]="!addLoanTypesForm.valid" (click)="addLoanType()">Add Loan</button>

我总是只有一个错误这个字段是必需的对于最小和最大长度验证从不显示。由于我的提交按钮在 from 有效之前被禁用,我可以看到按钮保持禁用状态,直到我满足条件。但是验证消息永远不会显示。我的代码有什么问题?

穆罕默德·坎通布尔

因为loanType只有required验证。loanName有一个minmax验证。如果addLoanTypesForm.get('loanName')检查而不是addLoanTypesForm.get('loanType').

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

反应形式的条件自定义验证不起作用-Angular 9

带有反应形式的 angular 4 表单验证不起作用

反应性表单验证不起作用

表单验证在 angular 中不起作用?

Angular 2表单验证不起作用

Angular 4 表单验证不起作用

Angular支持表单验证不起作用

Angular 11 和 .Net post 方法不起作用

Angular 2 of-class 在 IE 11 中不起作用

tsconfig.app.json 的别名不起作用 Angular 11

Angular 11 - 可重用组件不起作用

为什么在我的Angular项目中使用此反应性表单示例,验证程序规则似乎不起作用?

Angular 反应式表单自定义验证:更新更改第一次不起作用

Angular 7反应性表单生成器,自定义交叉验证器不起作用

Bootstrap4 验证在 IE 11 中不起作用

Angular 5,Angular Material:Datepicker验证不起作用

angular CLI 11 版本在 Internet Explorer 11 中不起作用

Angular 5反应形式-FormArray的patchValue不起作用

Angular 5 反应形式 valueChanges 不起作用

Angular 反应形式,确认电子邮件不起作用

来自注入服务的Angular共享属性不起作用

由angular-cli 1.0.0构建的Angular 4.0.0 App在IE11中不起作用

升级到 Angular 11 后,Angular Material Tooltip 不起作用

异步自定义验证器不起作用并在Angular 8中显示错误消息

反应输入验证在表单中不起作用

角反应形式输入验证在负载上不起作用

输入验证在反应 onSubmit 中不起作用?

打字稿反应道具验证不起作用

Angular程序要求的验证器不起作用