【问题标题】:validator in angular showing deprecation error in angular角度中的验证器显示角度中的弃用错误
【发布时间】:2021-03-05 00:44:45
【问题描述】:

下面是我的验证实用功能

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 代码运行良好..但现在 this.formBuilder.group 已被弃用..

请给我任何替代解决方案..

编辑:

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?
}

这是我的验证服务..是否可以在验证服务中进行验证..

【问题讨论】:

标签: angular typescript


【解决方案1】:

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

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 null;
        }

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

【讨论】:

  • @非常感谢...在您回答后...是否可以在验证服务中移动 Mustmatch 功能?请告诉我。,,
  • mustMatch(control: AbstractControl, matchingControl: AbstractControl) { // 如果验证失败,则在 matchingControl 上设置错误 if (control.value !== matchingControl.value) { matchingControl.setErrors({ mustMatch: true } );返回 { mustMatch: true }; } 否则 { 返回空值; } }
  • 是的,您可以将其移至验证服务,只要它是一个功能,它来自哪里就不是问题
猜你喜欢
  • 2021-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-26
  • 1970-01-01
  • 2022-11-11
相关资源
最近更新 更多