angular中的验证程序显示angular中的弃用错误

angular中的验证程序显示angular中的弃用错误,angular,typescript,Angular,Typescript,下面是我的验证函数 import { FormGroup } from '@angular/forms'; // custom validator to check that two fields match export function MustMatch(controlName: string, matchingControlName: string) { return (formGroup: FormGroup) => { const control =

下面是我的验证函数

import { FormGroup } from '@angular/forms';

// custom validator to check that two fields match
export function MustMatch(controlName: string, matchingControlName: string) {
    return (formGroup: FormGroup) => {
        const control = formGroup.controls[controlName];
        const matchingControl = formGroup.controls[matchingControlName];

        if (matchingControl.errors && !matchingControl.errors.mustMatch) {
            // return if another validator has already found an error on the matchingControl
            return;
        }

        // set error on matchingControl if validation fails
        if (control.value !== matchingControl.value) {
            matchingControl.setErrors({ mustMatch: true });
        } else {
            matchingControl.setErrors(null);
        }
    }
}
这是组件类

this.userForm = this.formBuilder.group({

      id: [0],  
      userName: ['', [Validators.required, Validators.minLength(3), Validators.maxLength(30)]],
      password: ['', [Validators.required, Validators.minLength(5), Validators.maxLength(30)]],
      confirmPassword: ['', [Validators.required, Validators.minLength(5), Validators.maxLength(30)]],
     
      ..
     
      }, {
        validator: MustMatch('password', 'confirmPassword')  --After adding this i am gtting deprecated error
    });
ablove代码运行正常..但是现在我得到了这个。formBuilder.group不推荐使用

请告诉我其他的解决办法

编辑:


这是我的验证服务..是否可以在验证服务中使用MACT..

您的验证程序函数需要返回
ValidationErrors|null

导出函数必须匹配(controlName:string,matchingControlName:string){
返回(formGroup:formGroup)=>{
const control=formGroup.controls[controlName];
const matchingControl=formGroup.controls[matchingControlName];
if(matchingControl.errors&&!matchingControl.errors.mustMatch){
//如果另一个验证器已在matchingControl上发现错误,则返回
返回null;
}
//如果验证失败,则在matchingControl上设置错误
if(control.value!==matchingControl.value){
matchingControl.setErrors({mustMatch:true});
返回{mustMatch:true};
}否则{
matchingControl.setErrors(空);
返回null;
}
}
}

这可能很有用,谢谢我已经检查了D。我没有得到..所以问..@非常感谢您…在您的回答很小之后…是否可以在验证服务中移动Mustmatch函数?请让我知道.,,mustMatch(control:AbstractControl,matchingControl:AbstractControl){//如果验证失败,则在matchingControl上设置错误,如果(control.value!==matchingControl.value){matchingControl.setErrors({mustMatch:true});返回{mustMatch:true};}else{return null;}}是的,您可以将它移动到验证服务,只要它是一个函数,它从哪里来就不会有问题
import { AbstractControl } from '@angular/forms';
import { Injectable } from '@angular/core';

@Injectable()
export class ValidationService {

    getValidatorErrorMessage(name: string, value?: any) {
        const config = {
            'required': 'Required',
            'email':'Invalid email address',            
            'invalidPassword': 'This regex can be used ',            
            'mustMatch': `Passwords must match`,
        };
        return config[name];
    }

    
    password(control: AbstractControl) {
        if (control.value.match(/^(?=[^\d_].*?\d)\w(\w|[!@#$%]){7,20}/)) {
            return null;
        } else {
            return { 'invalidPassword': true };
        }
    }
 //can i move mustmatch function here?
}