RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 900619
Accepted
Sergei R
Sergei R
Asked:2020-11-01 04:52:56 +0000 UTC2020-11-01 04:52:56 +0000 UTC 2020-11-01 04:52:56 +0000 UTC

如果字段具有有效值,角度材料如何手动将字段标记为无效(并将其重新着色为红色)

  • 772

注册表单,字段被填写并发送到服务器。服务器发送电子邮件已忙的响应。然后我想将电子邮件字段突出显示为无效,但该字段中的值支持电子邮件格式并被验证器识别为有效。

在此处输入图像描述

尝试了不同的变化

this.registerForm.controls['email'].setErrors({ invalid: true, valid: false })

但该领域没有着色。怎么做?

注册.component.ts

export class RegistrationComponent implements OnInit {

  public registerForm: FormGroup
  public spinnerShow = false
  public errMsg: String
  public bothErrMsg: String.

  constructor(
    ...
    private matDialogRef: MatDialogRef<RegistrationComponent>,
    private fb: FormBuilder
  ) {}

  ngOnInit() {
    this.registerForm = this.fb.group({
      email: ['', [FormValidationService.emailValidator]],
      password: this.fb.group({
        pwd: ['', [FormValidationService.passwordValidator]],
        confirm: ['', [FormValidationService.passwordValidator]],
      }, {validator: FormValidationService.confirmValidator('pwd', 'confirm')}),
      username: ['', [FormValidationService.shortStringValidator]],
    })
  }

  register() {
    this.spinnerShow = !this.spinnerShow
    const regData = this.registerForm.value
    regData.confirm = this.registerForm.value.password.confirm
    regData.password = this.registerForm.value.password.pwd

    this.auth.registration(regData).subscribe(
      (res: any) => {
        if (res.error) {
          this.spinnerShow = !this.spinnerShow
          const messages = res.messages
          for (const key in messages) {
            if (messages.hasOwnProperty(key)) {
              if (key === 'both') {
                this.bothErrMsg = messages[key]
                this.registerForm.controls['email'].setErrors({ notUnique: true })
              } else {
                this.registerForm.controls[key].setErrors({ errMsg: messages[key] })
              }
            }
          }
        } else {
          this.lc.setItem('user', res.user)
          this.matDialogRef.close()
        }
      },
      error => {
        console.log(error) // TODO
        this.spinnerShow = !this.spinnerShow
      }
    )
  }
}

注册.component.html

<div class="container">
  <h3>Register new user.</h3>
  <form [formGroup]="registerForm" novalidate>
    <mat-form-field>
      <input matInput formControlName="email" placeholder="Email" type="email" required>
      <mat-error *ngIf="registerForm.controls.email.hasError('errMsg') && registerForm.controls.email.touched">
        {{ registerForm.get('email').getError('errMsg') }}
      </mat-error>
    </mat-form-field>

    <div class="error" *ngIf="bothErrMsg !== undefined">{{ bothErrMsg }}</div>

    <button mat-raised-button color="primary" [disabled]="registerForm.invalid == true" (click)="register()">Sign Up</button>
  </form>
</div>

来自服务器的错误(两者都是 serverErrorMsg 尚未重命名为正常名称)。服务器返回 200 代码和错误消息(如果有)。 在此处输入图像描述

angular4
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    overthesanity
    2020-11-02T01:25:03Z2020-11-02T01:25:03Z

    您可能错误地使用了该指令matInput。如果控件具有 ,则输入会自动突出显示errors !== null。这是一个简单的例子:

    <form [formGroup]="registerForm">
        <mat-form-field>
            <input matInput placeholder="Email" formControlName="email">
            <mat-error *ngIf="email.dirty && email.hasError('notUnique')">This email is already taken!</mat-error>
        </mat-form-field>
    </form>
    

    在一个组件里面,你做的就够了subscribe,但是为什么你在里面处理错误还不清楚subscribe,如果服务器返回422(验证错误),那么这个必须在里面处理onError或者使用操作符catchError:

    public registerForm = new FormGroup({
        email: new FormControl(null, {
            validators: [Validators.required, Validators.email]
        })
    });
    
    public get email(): AbstractControl {
        return this.registerForm.get('email')!;
    }
    
    public register(): void {
        this.auth.registration(this.registerForm.value).subscribe((res) => {
            this.email.setErrors({ notUnique: true });
        });
    }
    
    • 1

相关问题

  • https://angular.io 网站上的链接如何工作

  • 为什么数据没有更新?

  • Angular2如何显示模态窗口?

  • 如何在 Angular 4 的 HTTP PUT 请求中发送表单数据?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5