角反应表格 - 自定义验证器呼叫服务
角度应用和反应性形式。 我需要验证每个创建的新记录的名称的唯一性。 我有这样做的后端端点,因此返回布尔值。 现在,我需要将其连接到表单字段,但找不到这样做的方式。 理想情况下,我需要一个自定义验证器,可以添加到现场验证器中,但任何解决方案实际上都适合。
这是表格:
groupForm : FormGroup = new FormGroup({
shortName: new FormControl(null, [Validators.required,
Validators.minLength(1), Validators.maxLength(10), Validators.pattern(this.shortNameRegex)]),
fullName: new FormControl(null, [Validators.required,
Validators.minLength(1), Validators.maxLength(100)]),
emailDomains: new FormControl(null, [Validators.required]),
addressLine1: new FormControl(null, []),
addressLine2: new FormControl(null, []),
townCity: new FormControl(null, []),
county: new FormControl(null, []),
eircode: new FormControl(null, [Validators.pattern(this.eircodeRegex)]),
ratingScales: new FormControl(null, []),
});
这是函数调用:
checkGroupUnique() {
this.onLenderGroupService.checkGroupUnique(this.groupForm.controls.shortName.value).subscribe(res => {
this.isGroupUnique = res;
});
}
但是我还没有在任何地方称呼它,我需要将此类验证器添加到名为“ Shortname”的第一个字段。
Angular application and reactive forms.
I have a requirement to validate the uniqueness of the name of every new record created.
I have back-end endpoint doing this and returning boolean value as a result.
Now I need to hook it up to the form field but I cannot find the way of doing it.
Ideally I need a custom validator I could add to the field validators but any solution will fit actually.
Here is the form:
groupForm : FormGroup = new FormGroup({
shortName: new FormControl(null, [Validators.required,
Validators.minLength(1), Validators.maxLength(10), Validators.pattern(this.shortNameRegex)]),
fullName: new FormControl(null, [Validators.required,
Validators.minLength(1), Validators.maxLength(100)]),
emailDomains: new FormControl(null, [Validators.required]),
addressLine1: new FormControl(null, []),
addressLine2: new FormControl(null, []),
townCity: new FormControl(null, []),
county: new FormControl(null, []),
eircode: new FormControl(null, [Validators.pattern(this.eircodeRegex)]),
ratingScales: new FormControl(null, []),
});
and here is the function call:
checkGroupUnique() {
this.onLenderGroupService.checkGroupUnique(this.groupForm.controls.shortName.value).subscribe(res => {
this.isGroupUnique = res;
});
}
however I do not call it anywhere yet, I need to add such validator to first field named "shortName".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以在Angular中创建异步验证器:
It is possible to create asynchronous validators in Angular: