angular2在表单初始化后添加自定义验证

时间:2017-07-06 14:36:40

标签: angular-reactive-forms angular-validation angular-forms

我在组件中设置自定义验证错误消息而不是html。我不能像错误消息对象那样获取全局变量,因为表单未初始化。获取未定义的错误消息...

组件

        messages = {
              'amount': { 'required': this.locale.translate('allocation.percentRequired'), 'minValue': this.locale.translate('allocation.minValue') }
          };

     this.transactionForm = this.fb.group({
          allocationForm: this.fb.group({
            fund: ['', Validators.required],
            amount: ['', [Validators.required, this.checkMinAllocations]],
          }),
        });

 checkMinAllocations(fieldControl: FormControl) {

    if (fieldControl.value != "" && fieldControl.value == 0) {
      const messages = this.messages['amount'];
      this.errors['amount'] += messages['minValue'] + ' ';
    }
  }

0 个答案:

没有答案