我如何在 Angular 2 形式中添加更多的验证器参数

How do i add more that one Validator argument in Angular 2 form

我已经尝试将多个 angular 验证器参数 [即 Validators.minLength(8) 和 Validators.maxLength(12) ] 添加到我的表单中,但我似乎无法让它工作。 ..这些参数附加到下面代码中的 (password 和 passwordc) 实例。有什么帮助吗?

   export class signupComponent {
        signupform: FormGroup;
        constructor(public fb: FormBuilder) {
            this.signupform = this.fb.group({
                firstname: ['', Validators.required],
                lastname: ['', Validators.required],
                account: this.fb.group({
                    email: ['', Validators.required],
                    **password: ['', Validators.minLength(8), Validators.maxLength(12), Validators.pattern('[A-Za-z]{5}')],
                    passwordc: ['', Validators.minLength(8), Validators.maxLength(12), Validators.pattern('[A-Za-z]{5}')],
                }, { validator: passwordMatcher }),**
                shortbio: ['', Validators.required]
            });

        }
    }

要支持多个验证器,您必须使用接受验证器数组的 Validators.compose( [ ] ) 方法。你的情况:

password: ['', Validators.compose([Validators.minLength(8), Validators.maxLength(12), Validators.pattern('[A-Za-z]{5}')])]
password: ['', [Validators.minLength(8), Validators.maxLength(12), Validators.pattern('[A-Za-z]{5}')]]

这应该可以为您完成工作。