角反应表格 - 自定义验证器呼叫服务

发布于 2025-02-01 10:26:33 字数 1183 浏览 3 评论 0原文

角度应用和反应性形式。 我需要验证每个创建的新记录的名称的唯一性。 我有这样做的后端端点,因此返回布尔值。 现在,我需要将其连接到表单字段,但找不到这样做的方式。 理想情况下,我需要一个自定义验证器,可以添加到现场验证器中,但任何解决方案实际上都适合。

这是表格:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍白女子 2025-02-08 10:26:33

可以在Angular中创建异步验证器:

groupForm = new FormGroup({
  shortName: new FormControl(null, [Validators.required, ..., this.checkGroupUnique.bind(this)]),

...

checkGroupUnique(control: AbstractControl): Observable<ValidationErrors | null> {
  return this.onLenderGroupService.checkGroupUnique(control.value).pipe(
    map(isUnique => {
      if (isUnique) {
        return null; // no error
      } else {
        return { isGroupUnique: true };
      }
    })
  );
}
<label *ngIf="groupForm.controls.shortName.errors?.isGroupUnique">
  Group name is not unique!
</label>

It is possible to create asynchronous validators in Angular:

groupForm = new FormGroup({
  shortName: new FormControl(null, [Validators.required, ..., this.checkGroupUnique.bind(this)]),

...

checkGroupUnique(control: AbstractControl): Observable<ValidationErrors | null> {
  return this.onLenderGroupService.checkGroupUnique(control.value).pipe(
    map(isUnique => {
      if (isUnique) {
        return null; // no error
      } else {
        return { isGroupUnique: true };
      }
    })
  );
}
<label *ngIf="groupForm.controls.shortName.errors?.isGroupUnique">
  Group name is not unique!
</label>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文