1

我在组件而不是 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'] + ' ';
    }
  }
4

0 回答 0