Form Invalid Input Conditional Coloring Angular/Materialize

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

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)

<form [formGroup]="reactiveForm" (ngSubmit)="submit()">
    <mat-form-field >
        <input matInput [formControl]="firstName" type="text">
        <mat-error>Error</mat-error>
    </mat-form-field>
</form>

组件(.ts)

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

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

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

我尝试过的事情:

在输入中添加required关键字:

<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)

&lt;form [formGroup]=&quot;reactiveForm&quot; (ngSubmit)=&quot;submit()&quot;&gt;
    &lt;mat-form-field &gt;
        &lt;input matInput [formControl]=&quot;firstName&quot; type=&quot;text&quot;&gt;
        &lt;mat-error&gt;Error&lt;/mat-error&gt;
    &lt;/mat-form-field&gt;
   &lt;/form&gt;

Component (.ts)

    export class PersonFormComponent {
    
    firstName = new FormControl(null, Validators.required)

      reactiveForm: FormGroup = new FormGroup({firstName: this.firstName});
}

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

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

What I have tried:

Adding required keyword to the input:

&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:

<form [formGroup]="reactiveForm" (ngSubmit)="submit()">
    <mat-form-field >
        <input matInput [formControl]="firstName" type="text" 
            [class.ng-touched]="firstName.touched && !firstName.focus" 
            [class.ng-untouched]="!firstName.touched">
        <mat-error>Error</mat-error>
    </mat-form-field>
</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:

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

答案2

得分: 0

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

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

You are missing the required attribute on the input tag.

&lt;mat-form-field&gt;
    &lt;input matInput [formControl]=&quot;firstName&quot; type=&quot;text&quot; required&gt;
    &lt;mat-error&gt;Error&lt;/mat-error&gt;
&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:

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


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

Changed the .html to:

    &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:

确定