简体   繁体   English

Angular:使用浏览器的 HTML 本机构建验证错误消息

[英]Angular: using browser's HTML native build in validation error messages

Is there a way to use browser's native HTML validation error messages and use them in angular way?有没有办法使用浏览器的本机 HTML 验证错误消息并以 angular 方式使用它们?

what I would like is to when having like a reactive form like this:我想要的是像这样的反应形式:

testForm: FormGroup = this.fb.group({
  postCode: [{ value: 'postcode', disabled: true }],
  name: ['John'],
  city: [{ value: 'London', disabled: false }, [Validators.required, Validators.minLength(3)]],
});

to be able to use the native error messages in the form like:能够以如下形式使用本机错误消息:

<form [formGroup]="testForm" #myForm="ngForm">
  <input type="text" formControlName="city" >
  <div class="errors">
    <p *ngFor="let errorCode in myForm.controls.city.errors">
      {{ getBrowserNativeErrorMessage(errorCode) }}
    </p>
  </div>
</form>

You can use ngNativeValidate attribute in the form tag您可以在form标签中使用ngNativeValidate属性

<form ngNativeValidate>
</form>

But I don't think Reactive form Validators will work with this, you will have to use html native validation attributes但我不认为 Reactive form Validators会用这个,你将不得不使用 html native validation attributes

<input type="text" minlength="3" required>

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

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