简体   繁体   English

文本区域上的模板驱动表单验证

[英]Template Driven Form Validation on Text area

I added a text area to my angular form.我在我的角度表单中添加了一个文本区域。 all the input/mat-select fields work fine with the requried attribute.所有 input/mat-select 字段都可以与 requried 属性一起正常工作。 For the textarea I added a custom class to show a red border.对于textarea我添加了一个自定义类来显示红色边框。 For some reason when I click the clear button the two input fields automatically applies its own red class but my custom class for the texarea a does not unless it has been touched.出于某种原因,当我单击清除按钮时,两个输入字段会自动应用其自己的红色类,但我的texarea a 自定义类不会,除非它已被触摸。 Which is fine but does not match the styles of the input fields.这很好,但与输入字段的样式不匹配。 How do I apply the same validation styles to the text area: https://stackblitz.com/edit/angular-mpefsn如何将相同的验证样式应用于文本区域: https : //stackblitz.com/edit/angular-mpefs​​n

.text-error.ng-invalid.ng-touched{
  border: 1px solid #b00b00;
}

If you click on the clear button initially when the stack blitz loads you will see the two inputfields turn red but not the textarea .如果您在堆栈闪电战加载时最初单击清除按钮,您将看到两个输入字段变为红色,但不是textarea This is because I have the ngtouched class.这是因为我有ngtouched类。 But if I remove it then the textarea will initially be red.但是如果我删除它,那么 textarea 最初将是红色的。

The easiest way would to just use matInput with the textarea最简单的方法是将 matInput 与 textarea 一起使用

<mat-form-field>
  <textarea matInput ...></textarea>
</mat-form-field>

then you would have the same style for all the input fields.那么所有输入字段都将具有相同的样式。 And material would apply red because it is required.并且材料会应用红色,因为它是必需的。

The harder way, if you want the textarea to be of different style could be to manually add a class to the textarea field.更难的方法是,如果您希望 textarea 具有不同的样式,可以手动向 textarea 字段添加一个类。

.ts file: .ts 文件:

public cleared = false;
...
clear() {
  ..
  this.cleared = true;
}

and in your html:并在您的 html 中:

<textarea [(ngModel)]="inputValue" [class.custom_invalid_form_class]="cleared && inputValue.length > 0"></textarea>

You could mark the textarea form control as touched when clicking on the clear button:单击清除按钮时,您可以将textarea表单控件标记为已touched

<textarea #txt="ngModel" [(ngModel)]="questionText" required ...></textarea>

<button (click)="clear(); txt.control.markAsTouched();" ...>Clear</button>

See this stackblitz for a demo.有关演示,请参阅此 stackblitz

The reason you are seeing the red border here for the other fields except for the textarea is because the other fields are getting their CSS from .mat-form-field-invalid .您在此处看到除textarea之外的其他字段的红色边框的原因是因为其他字段从.mat-form-field-invalid获取其CSS The .mat-form-field-invalid gets added to your mat-form-field because of this class which gets added when you submit the form without filling the required fields. .mat-form-field-invalid会添加到您的mat-form-field因为此类会在您提交表单而不填写必填字段时添加。

So do the same for your textarea , remove the text-error class as it's not required and add the matInput directive.所以对你的textarea做同样的事情,删除text-error类,因为它不是必需的,并添加matInput指令。 Then just wrap your textarea in a mat-form-field like below然后只需将您的textarea包裹在如下所示的mat-form-field

<mat-form-field>
    <textarea matInput name="questionText" cols="35" rows="8" placeholder="Question Text" [(ngModel)]="questionText" required></textarea>
</mat-form-field>

Here is a working example on StackBlitz .这是StackBlitz上的一个工作示例。

Use the使用

<ion-textarea></ion-textarea>

instead of the native而不是本地人

<textarea></textarea>

in order to see validation results like with the为了查看验证结果,例如

<ion-input></ion-input>

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

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