【问题标题】:Pass a form field value in the same object在同一对象中传递表单字段值
【发布时间】:2018-05-23 22:29:59
【问题描述】:

我正在尝试向自定义表单验证器发送值:

this.myForm = this.fb.group({
            Id: [null],
            Password: ['', [Validators.required]],
            PasswordConfirm: ['', Validators.compose([CustomValidators.expectedMatchingFields(this.myForm.controls.Password.value), Validators.required])]
        });

但它不喜欢我发送"this.myForm.controls.Password.value"。还有其他方法吗?

"Cannot read property 'controls' of undefined"会出错。

【问题讨论】:

  • “不喜欢我发送”是什么意思?有没有报错?
  • 您能否提供更多信息,例如什么错误或验证器是如何实现的?

标签: angular


【解决方案1】:

调用时this.myForm为空

CustomValidators.expectedMatchingFields(this.myForm.controls.Password.value)

在 this.fb.group() 被调用之前你不能这样做。 我的建议:创建没有此验证器的表单,然后更新它,如 here 所述。

我想这可能有用

this.myForm = this.fb.group({
 Id: [null],
 Password: ['', [Validators.required]],
 PasswordConfirm: ['']

});

this.myForm.controls.PasswordConfirm
.setValidators(
Validators.compose(
[CustomValidators.expectedMatchingFields(
  this.myForm.controls.Password.value),
  Validators.required
]));

【讨论】:

  • 非常感谢 :)
猜你喜欢
  • 1970-01-01
  • 2023-03-14
  • 2017-11-16
  • 1970-01-01
  • 2021-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-27
相关资源
最近更新 更多