简体   繁体   English

带有验证错误的 Angular6 Material 自定义表单字段控件(mat-error)

[英]Angular6 Material custom form field control with validation errors (mat-error)

How to make a Material custom form field control ( like this one ) to support form validation and display them with mat-error?如何制作 Material 自定义表单字段控件( 如这个)以支持表单验证并使用 mat-error 显示它们? I understand that the regular matInput directive uses ErrorStateMatcher ( doc ) but I don't understand how I can link it with a custom form field.我知道常规 matInput 指令使用 ErrorStateMatcher ( doc ),但我不明白如何将其与自定义表单字段链接。

By looking at some existing component from Material2 ( https://github.com/angular/components/blob/master/src/material/select/select.ts ), I found a solution.通过查看 Material2 ( https://github.com/angular/components/blob/master/src/material/select/select.ts ) 中的一些现有组件,我找到了一个解决方案。 I created a base class following this example我按照这个例子创建了一个基类

export const _MatSelectMixinBase:
CanDisableCtor &
HasTabIndexCtor &
CanDisableRippleCtor &
CanUpdateErrorStateCtor &
typeof MatSelectBase =
    mixinDisableRipple(mixinTabIndex(mixinDisabled(mixinErrorState(MatSelectBase))));

I had to copy from the Material repo some types like CanUpdateErrorStateCtor.我不得不从 Material repo 中复制一些类型,比如 CanUpdateErrorStateCtor。

Then update my constructor to inject a ErrorStateMatcher and finally in ngDoCheck, do this:然后更新我的构造函数以注入 ErrorStateMatcher,最后在 ngDoCheck 中执行以下操作:

ngDoCheck() {
   if (this.ngControl) {
      this.updateErrorState();
   }
}

If you followed the official guide to create a custom material form field, and you have declared ngControl in constructor:如果您按照官方指南创建自定义材料表单字段,并且您已经在构造函数中声明了ngControl

  constructor(
    ...,
    @Optional() @Self() public ngControl: NgControl) {
    ...

    if (this.ngControl != null) {
      this.ngControl.valueAccessor = this;
    }
  }

all you need is:所有你需要的是:

  get errorState(): boolean {
    return this.ngControl.invalid && this.ngControl.dirty;
  }

to get validation working.使验证工作。

you can check that from the FocusMonitor in the example ,it can be something like this:您可以从示例中的FocusMonitor中检查,它可以是这样的:

  fm.monitor(elRef.nativeElement, true).subscribe(origin => {
    if (this.parts.status === 'INVALID') {
      this.errorState = true;
    }
    this.focused = !!origin;
    this.stateChanges.next();
  });

the idea is material provide errorState , you can see that from the component's type, so whenever you change it, it will reflect on the component, hope it's help!这个想法是 material 提供errorState ,你可以从组件的类型中看到,所以每当你改变它时,它都会反映在组件上,希望对你有所帮助!

The solution is correct!解决方法是正确的! Reusing of mixinErrorState from material core is the best way to handle it.重用材料核心中的 mixinErrorState 是最好的处理方式。 I recently released a detailed video about custom form field where also gave detailed explanation to error handling in custom form fields.我最近发布了一个关于自定义表单字段的详细视频,其中还详细解释了自定义表单字段中的错误处理。 It might be interesting for others who understands better by watching video https://youtu.be/AZsw2nRxkBk?t=825对于其他通过观看视频更好地理解的人来说可能会很有趣https://youtu.be/AZsw2nRxkBk?t=825

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

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