更改表单以动态方式控制验证并立即以角反应形式应用新验证

时间:2019-04-24 06:34:47

标签: angular validation angular-reactive-forms

我有几种形式,我需要根据#form1用户选择对#form2输入进行自定义验证。我写了一个自定义验证(尚未完成)。

自定义验证程序:

@Injectable()
export class Checks {

    constructor(public prescConfigService: PrescConfigService) { }

    validity(section, key: string): ValidatorFn | null {
        const condition = this.prescConfigService.getDefaultSetting(section, key);
        if (!condition) {
            console.log('if there is no condition (not selected): ', condition);
            return (control: AbstractControl): { [key: string]: boolean } | null => {
                console.log('control1: ', control);
                return null;
            };
        } else {
            console.log('elseeeee', condition);
            return (control: AbstractControl): { [key: string]: boolean } | null => {
                console.log('control2: ', control);
                return { 'required': true };
            };
        }
    }
}

form2创建:

this.form = this.fb.group({
      NationalNum: [this.prescConfigService.getValueByField('CustomerNationalNum'), {
        validator: [this.checks.validity('insurance', 'CheckNationalNum')],
        updateOn: 'change'
      }],
      BirthDateJalali: [this.prescConfigService.getValueByField('BirthDate'), Validators.required],
      Cellphone: [this.prescConfigService.getValueByField('Cellphone'), Validators.required],
      Sex: [this.prescConfigService.getValueByField('SexID'), Validators.required],
    });

我需要做的是,当用户从#form1(位于不同组件上)更改选择时,将应用自定义验证。反正有吗?

0 个答案:

没有答案