Form Invalid Input Conditional Coloring Angular/Materialize

huangapple go评论104阅读模式
英文:

Form Invalid Input Conditional Coloring Angular/Materialize

问题

我正在使用Angular中的响应式表单,尝试在输入字段被触摸、失去焦点并且为空时更改输入字段的颜色。我查看了material.angular.io的文档并实施了它,但颜色仍然没有像文档中那样改变。我可以看到<mat-error>元素,但颜色没有改变。有没有一种非 hacky 的方法来改变颜色?

文档来源:
https://material.angular.io/components/form-field/overview#form-field-error

组件(.html)

  1. <form [formGroup]="reactiveForm" (ngSubmit)="submit()">
  2. <mat-form-field >
  3. <input matInput [formControl]="firstName" type="text">
  4. <mat-error>Error</mat-error>
  5. </mat-form-field>
  6. </form>

组件(.ts)

  1. export class PersonFormComponent {
  2. firstName = new FormControl(null, Validators.required)
  3. reactiveForm: FormGroup = new FormGroup({firstName: this.firstName});
  4. }

我已在app.module.ts的@NgModule的导入中声明了MatInputModule:

  1. import { MatInputModule } from '@angular/material/input';

我尝试过的事情:

在输入中添加required关键字:

  1. <input matInput [formControl]="firstName" type="text" required>
英文:

I'm using Reactive Forms in Angular and I'm trying to change the color of the input field if the input is touched and unfocused and left empty.

I was looking through the documentation of material.angular.io and implemented it but it still doesn't change the color like in the documentation?

I can see the < mat-error> element but the color does not change. Is there a non-hacky way of doing the colors?

Documentation source:
https://material.angular.io/components/form-field/overview#form-field-error

Component (.html)

  1. &lt;form [formGroup]=&quot;reactiveForm&quot; (ngSubmit)=&quot;submit()&quot;&gt;
  2. &lt;mat-form-field &gt;
  3. &lt;input matInput [formControl]=&quot;firstName&quot; type=&quot;text&quot;&gt;
  4. &lt;mat-error&gt;Error&lt;/mat-error&gt;
  5. &lt;/mat-form-field&gt;
  6. &lt;/form&gt;

Component (.ts)

  1. export class PersonFormComponent {
  2. firstName = new FormControl(null, Validators.required)
  3. reactiveForm: FormGroup = new FormGroup({firstName: this.firstName});
  4. }

I have declared MatInputModule in the imports of @NgModule in app.module.ts:

  1. import { MatInputModule } from &#39;@angular/material/input&#39;;

What I have tried:

Adding required keyword to the input:

  1. &lt;input matInput [formControl]=&quot;firstName&quot; type=&quot;text&quot; required &gt;

答案1

得分: 0

I would have tried to use custom classes according to the status of touched and focus to change the color by custom ones:

  1. <form [formGroup]="reactiveForm" (ngSubmit)="submit()">
  2. <mat-form-field >
  3. <input matInput [formControl]="firstName" type="text"
  4. [class.ng-touched]="firstName.touched && !firstName.focus"
  5. [class.ng-untouched]="!firstName.touched">
  6. <mat-error>Error</mat-error>
  7. </mat-form-field>
  8. </form>

(Note: The code part has been left unchanged as per your request.)

英文:

I would have tried to use custom classes according to the status of touched and focus to change the color by custom ones:

  1. &lt;form [formGroup]=&quot;reactiveForm&quot; (ngSubmit)=&quot;submit()&quot;&gt;
  2. &lt;mat-form-field &gt;
  3. &lt;input matInput [formControl]=&quot;firstName&quot; type=&quot;text&quot;
  4. [class.ng-touched]=&quot;firstName.touched &amp;&amp; !firstName.focus&quot;
  5. [class.ng-untouched]=&quot;!firstName.touched&quot;&gt;
  6. &lt;mat-error&gt;Error&lt;/mat-error&gt;
  7. &lt;/mat-form-field&gt;
  8. &lt;/form&gt;

答案2

得分: 0

你的输入标签缺少必需的属性。

  1. <mat-form-field>
  2. <input matInput [formControl]="firstName" type="text" required>
  3. <mat-error>Error</mat-error>
  4. </mat-form-field>
英文:

You are missing the required attribute on the input tag.

  1. &lt;mat-form-field&gt;
  2. &lt;input matInput [formControl]=&quot;firstName&quot; type=&quot;text&quot; required&gt;
  3. &lt;mat-error&gt;Error&lt;/mat-error&gt;
  4. &lt;/mat-form-field&gt;

答案3

得分: 0

I changed the .ts to:

reactiveForm: FormGroup = this.formBuilder.group({
firstName: ["", Validators.required]
});

isFirstNameValid() {
if (this.reactiveForm.get("firstName")?.touched && this.reactiveForm.get("firstName")?.valid == false) {
return false;
}
return true;
}

Changed the .html to:

<input formControlName="firstName" matInput type="text" [ngClass]="{'ng-invalid': isFirstNameValid()}">

Now it works as expected.

英文:

I changed the .ts to:

  1. reactiveForm: FormGroup = this.formBuilder.group({
  2. firstName : [&quot;&quot;, Validators.required]
  3. });
  4. isFirstNameValid(){
  5. if(this.reactiveForm.get(&quot;firstName&quot;)?.touched &amp;&amp; this.reactiveForm.get(&quot;firstName&quot;)?.valid == false){
  6. return false;
  7. }
  8. return true;
  9. }

Changed the .html to:

  1. &lt;input formControlName=&quot;firstName&quot; matInput type=&quot;text&quot; [ngClass]=&quot;{&#39;ng-invalid&#39; : isFirstNameValid()}&quot;&gt;

Now it works as expected.

huangapple
  • 本文由 发表于 2023年4月19日 15:07:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76051629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定