简体   繁体   English

返回{__zone_symbol__state:null,__ zone_symbol__value:Array(0)}的Angular自定义异步验证器

[英]Angular Custom Async Validator returning {__zone_symbol__state: null, __zone_symbol__value: Array(0)}

Am trying to implement custom validators. 我试图实现自定义验证器。 The non-async one ( cannotContainSpaces ) works just fine. 非异步的( cannotContainSpaces )工作得很好。 The async one ( shouldBeUnique ), which, yes, is trivial at the moment, should be returning promise as I understand, which the Validator object should resolve. 异步的( shouldBeUnique ),其中,是的,目前是微不足道的,应该按照我的理解返回promise,Validator对象应该解析。 It doesn't. 它没有。 The errors collection on the formControl username shows this in the console: formControl username上的错误集合在控制台中显示:

{__zone_symbol__state: null, __zone_symbol__value: Array(0)}

Form component: 表单组件:

import { CustomValidators } from './custom.validators';
import { Component, Input } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';

@Component({
  selector: 'signup-form',
  templateUrl: './signup-form.component.html',
  styleUrls: ['./signup-form.component.css']
})
export class SignupFormComponent {

  form = new FormGroup({
    username: new FormControl('', [ 
       CustomValidators.cannotContainSpaces,
       CustomValidators.shouldBeUnique
      //  Validators.email, 
    ]),
    password: new FormControl('', Validators.required)
  })

  get username() {
    return this.form.get('username');
  }

  keyPressed(){
    console.log(this.username.errors)
  }

}

Custom validator method: 自定义验证方法:

import { AbstractControl, ValidationErrors } from "@angular/forms";

export class CustomValidators {
    static cannotContainSpaces(control: AbstractControl) : ValidationErrors | null {
        if ((<string>control.value).indexOf(' ') >= 0)
            return { cannotContainSpaces: true};
        return null;
    }

    static shouldBeUnique(control: AbstractControl) : Promise<ValidationErrors | null> {
        return new Promise((resolve, reject) => {
            setTimeout(function() {
                if (control.value === 'treve')
                    resolve({shouldBeUnique: true});
                else resolve(null);
            }, 2000);
        });
    }
}

Relevant HTML: 相关HTML:

<form [formGroup]="form">
    <div class="form-group">
        <label for="username">Username</label>
        <input 
            (keyup) = "keyPressed()" (blur) = "keyPressed()"
            formControlName="username"
            id="username" 
            type="text" 
            class="form-control">
        <div *ngIf="username.touched && username.invalid" class="alert alert-danger">
            <div *ngIf="username.errors.cannotContainSpaces">Username must not contain spaces</div>
            <div *ngIf="username.errors.shouldBeUnique">Sorry, that username has been taken</div>
        </div>
    </div>

Async validators are to be set as the third argument: 异步验证器将被设置为第三个参数:

username: ['', [sync validators here], [async validators here]]

so change the the following: 所以改变以下内容:

username: new FormControl('', 
  [ 
   CustomValidators.cannotContainSpaces,
   CustomValidators.shouldBeUnique
  ]),

to: 至:

username: new FormControl('', 
   [CustomValidators.cannotContainSpaces],
   [CustomValidators.shouldBeUnique]
),

DEMO: http://plnkr.co/edit/OceHbSl3atPHdcvNRQDs?p=preview 演示: http : //plnkr.co/edit/OceHbSl3atPHdcvNRQDs?p=preview

暂无
暂无

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

相关问题 Angular 4 验证器设置错误“错误:{__zone_symbol__state:null,__zone_symbol__value:Array(0)}” - set error “ errors : {__zone_symbol__state: null, __zone_symbol__value: Array(0)}” by Angular 4 validator ionic 2 returns {“__ zone_symbol__state”:null,“__ zone_symbol__value”:“cyprus”} - ionic 2 returning {“__zone_symbol__state”:null,“__zone_symbol__value”:“cyprus”} 嗨,我正在尝试获取报价的价格,但是每当我返回任何函数时,总是使用__zone_symbol__state和__zone_symbol__value获取值 - Hi, I'm trying to get value of my ticks but whenever i return any function always getting value with __zone_symbol__state and __zone_symbol__value 带有 __zone_symbol__state 的 Angular 5 表单组错误:true - Angular 5 form group errors with __zone_symbol__state: true 如何在 Angular 2 中访问 ZoneAwarePromise 上的 __zone_symbol__ 值 - how can I access the __zone_symbol__value on the ZoneAwarePromise in Angular 2 如何从 API 调用的返回值中删除 __zone_symbol__value? - How to get rid of __zone_symbol__value from returning value from API call? 如何在不使用 __zone_symbol__value 属性的情况下从 localStorage 检索值 - how to retrieve a value from localStorage without using __zone_symbol__value property 离子函数返回对象t中的值和存储在__zone_symbol__value中的值: - Ionic function return value in object t and value stored in __zone_symbol__value: 我无法访问“ __zone_symbol”对象中的异步验证器 - I'm not able to access my Async Validator as it comes in “__zone_symbol” object 路由语法错误区域符号错误角度2 - Routing syntax error zone symbol error angular 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM