我在组件中设置自定义验证错误消息而不是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'] + ' ';
}
}