изменение формы контролирует валидацию, динамически и применяет новую валидацию немедленно в угловых реактивных формах - PullRequest
1 голос
/ 24 апреля 2019

У меня есть несколько форм, и мне нужна пользовательская проверка ввода # form2, основанная на выборе пользователя # form1.Я написал пользовательскую проверку (она еще не завершена).

Пользовательский валидатор:

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

Создание формы 2:

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 (который находится на другом компоненте).Есть ли вообще?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...