gpt4 book ai didi

angular - 在 Angular 验证器中链接两个字段

转载 作者:行者123 更新时间:2023-12-05 02:54:28 24 4
gpt4 key购买 nike

我有一个 Angular 9 表单,其中有四个相关字段。一个是复选框,其余是输入。选中复选框时,输入不应为空,但未选中时,则无所谓。我想为此制作验证器,以便仅当字段为空且第一个字段设置为 true 时才会出现错误。

我还考虑过创建一个表示复选标记状态的本地 bool 值,并像这样将其传递给验证器。

export function linkedFieldValidator(toggler: boolean): ValidatorFn {
console.log('updated');
return (control: AbstractControl): {[key: string]: any} | null => {
return (toggler && control.value === '') ? {linkedField: {value: control.value}} : null;
};
}

...
field: new FormControl('', linkedFieldValidator(this.checkboxvalue)),
...

但是,我想这是行不通的,因为它只传递 bool 值一次,之后不会更新。即使调用 updateValueAndValidity() 也不起作用,这对我来说很奇怪(如果不是这样,那么它的目的是什么?)。

我的 FormGroup 的结构看起来像这样:

this.form = this.formBuilder.group({
name: new FormControl(''), // don't care
address: new FormControl(''), // don't care
car: new FormControl(false), // do care - this is the checkmark
license_plate: new FormControl('', Validators.pattern(MY_LICENSE_PLATE_REGEX)), // shouldn't be empty when car
mileage: new FormControl('') // shouldn't be empty when car
hair: new FormControl(false), // do care - this is the checkmark
hair_color: new FormControl(''), // shouldn't be empty when hair
});

如您所见,我有几个 FormControll 相互关联,我只希望将其中的几个链接起来。另一个需要注意的重要事项是,如果违反其中一个条件,整个表单可能会变得无效,但我希望能够单独解决每个错误,以便我可以在适当的位置显示适当的消息。

我没有更多的想法,任何人都可以帮助我吗?我正在使用响应式表单。

最佳答案

问题是您只将初始值传递给 linkFieldValidator 函数。

为了动态获得值,您可以通过 FormGroup 传递 linkFieldValidator,如下所示:

readonly formGroup = this.formBuilder.group(
{
checkbox: '',
name: ''
},
{ validator: linkedFieldValidator }
);

完整示例:

import { ChangeDetectionStrategy, Component } from '@angular/core';
import {
AbstractControl,
FormBuilder,
FormGroup,
ValidationErrors,
ValidatorFn,
} from '@angular/forms';

export const linkedFieldValidator = (formGroup: FormGroup): ValidationErrors | null => {
const [checkboxFormControlValue, nameFormControlValue] = [
formGroup.get('checkbox')!.value,
formGroup.get('name')!.value
];

return checkboxFormControlValue && !nameFormControlValue
? { linkedField: { value: nameFormControlValue } }
: null;
};

@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'input-overview-example',
styleUrls: ['input-overview-example.css'],
templateUrl: 'input-overview-example.html'
})
export class InputOverviewExample {
readonly formGroup = this.formBuilder.group(
{
checkbox: '',
name: ''
},
{ validator: linkedFieldValidator }
);

constructor(private readonly formBuilder: FormBuilder) {}
}

DEMO


编辑 1:如果您需要将错误驻留在每个表单控件中,您可以将 linkedFieldValidator 更改为:

export const linkedFieldValidator = (formGroup: FormGroup): null => {
const { value: checkboxFormControlValue } = formGroup.get('checkbox')!;
const inputFormControls = [
formGroup.get('input1')!,
formGroup.get('input2')!,
formGroup.get('input3')!,
];

inputFormControls.forEach(inputFormControl => {
const { value } = inputFormControl;
const errors = checkboxFormControlValue && !value ? { linkedField: { value } } : null;
inputFormControl.setErrors(errors);
});

return null;
};

请注意,如果您需要保留其他错误,您可能需要在setErrors之前做一些处理。

DEMO

编辑 2:

对于可以有多个链接字段的通用方法,您可以这样做:

type LinkedFormControl = Record<string, string | readonly string[]>;

const arrayify = <T>(itemOrItems: T | readonly T[]): readonly T[] => {
return Array.isArray(itemOrItems) ? itemOrItems : [itemOrItems];
};

const getErrorObjectSanitized = <T extends object>(obj: T): T | null => {
return Object.keys(obj).length === 0 ? null : obj;
};

const getErrorsFor = (
checkerValue: boolean,
formControl: FormControl,
): object | null => {
const { errors, value } = formControl;
const { error, ...oldErrors } = errors || {};
const processedErrors = {
...(checkerValue && !value ? { error: true } : {}),
...oldErrors,
};

return getErrorObjectSanitized(processedErrors);
};

export const linkedFieldValidator = (linkedFormControls: LinkedFormControl) => {
return (formGroup: FormGroup): ValidationErrors | null => {
Object.keys(linkedFormControls).forEach(key => {
const { value: checkerValue } = formGroup.get(key)!;
const dependentKeys = arrayify(linkedFormControls[key]);

dependentKeys
.map(dependentKey => formGroup.get(dependentKey)!)
.forEach((dependentFormControl: FormControl) => {
dependentFormControl.setErrors(
getErrorsFor(checkerValue, dependentFormControl),
);
});
});

return null;
};
};

...调用将是这样的:

{
validator: linkedFieldValidator({
car: ['license_plate', 'mileage'],
hair: 'hair_color',
}),
},

DEMO

关于angular - 在 Angular 验证器中链接两个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61805637/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com