angular - FormBuilder 组已被验证器弃用

标签 angular typescript angular-reactive-forms

我有类似的问题:
How do I resolve the error I am encountering using custom Validator syntax?
FormBuilder group is deprecated
我已经阅读了问题,但问题仍然出现在 linter 中:

group is deprecated: This API is not typesafe and can result in issues with Closure Compiler renaming. Use the FormBuilder#group overload with AbstractControlOptions instead. Note that AbstractControlOptions expects validators and asyncValidators to be valid validators. If you have custom validators, make sure their validation function parameter is AbstractControl and not a sub-class, such as FormGroup. These functions will be called with an object of type AbstractControl and that cannot be automatically downcast to a subclass, so TypeScript sees this as an error. For example, change the (group: FormGroup) => ValidationErrors|null signature to be (group: AbstractControl) => ValidationErrors|null. (deprecation)


这是我的 formBuilder:
registerForm = this._fb.group({
        firstname: ["", [Validators.required]],
        lastname: ["", [Validators.required]],
        email: ["", [Validators.required, Validators.email]],
        password: ["", [PasswordValidator.strength]],
        confirmPassword: ["", [Validators.required]]
    }, {
        validator: PasswordValidator.confirmed("password", "confirmPassword")
    });
还有我的验证器:
export class PasswordValidator {
    static confirmed = (controlName: string, matchingControlName: string) => {
        return (formGroup: FormGroup) => {
            const control = formGroup.controls[controlName];
            const matchingControl = formGroup.controls[matchingControlName];

            if (matchingControl.errors && !matchingControl.errors.confirmedValidator) {
                return null;
            }

            if (control.value !== matchingControl.value) {
                matchingControl.setErrors({ confirmedValidator: true });
                return ({ confirmedValidator: true });
            } else {
                matchingControl.setErrors(null);
                return null;
            }
        };
    }
}
知道为什么吗?我正在返回正确的对象。 :/

最佳答案

@OwenKelvin 部分正确。必须是validators相反 validator在 formBuilder 中,即使它只是一个验证器,再次对不起 Owen。
但第二个问题是在验证器中。该函数应接收 AbstractControl相反 FormGroup .所以下面的代码是正确的:

export class PasswordValidator {
    static confirmed = (controlName: string, matchingControlName: string) => {
        return (control: AbstractControl): ValidationErrors | null => {
            const input = control.get(controlName);
            const matchingInput = control.get(matchingControlName);

            if (input === null || matchingInput === null) {
                return null;
            }

            if (matchingInput?.errors && !matchingInput.errors.confirmedValidator) {
                return null;
            }

            if (input.value !== matchingInput.value) {
                matchingInput.setErrors({ confirmedValidator: true });
                return ({ confirmedValidator: true });
            } else {
                matchingInput.setErrors(null);
                return null;
            }
        };
    }
}

关于angular - FormBuilder 组已被验证器弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65814492/

相关文章:

javascript - 修改后仍保留原始值

angular - 如何禁用 Angular 7 中的按钮

html - 为什么我在 Angular react 形式的 *ngIf 中出现错误

javascript - & 符号 (&) 替换为 Cookie 中的 %26 值

angular - 执行 ng update -g @angular/cli 时出现错误

javascript - *ngFor 在获取新数据之前正在更新

Angular5 react 形式 : Form array inside another form array

css - Angular Material 中的中心文本 MatButtonToggle

javascript - 类型 'T' 上不存在属性 - 一般问题

typescript ,单元测试 : Correct typing for Dispatching a Thunk with store. 从 redux-mock-store 发送