<form nz-form [nzLayout]="'vertical'" [formGroup]="validateForm">
<nz-form-item>
<nz-form-label nzRequired> 标准问题</nz-form-label>
<nz-form-control [nzErrorTip]="topicErrorTipTpl">
<nz-textarea-count [nzMaxCharacterCount]="50">
<textarea rows="2" nz-input formControlName="title" placeholder="请填写问题"></textarea>
</nz-textarea-count>
<ng-template #topicErrorTipTpl let-control>
<ng-container *ngIf="control.errors?.required">字段不能为空!</ng-container>
<ng-container *ngIf="control.errors?.invalidMaxLength">长度不超过50个字符! </ng-container>
</ng-template>
</nz-form-control>
</nz-form-item>
</form>
this.validateForm = this.fb.group({
title: ['', [Validators.required, proValidators.maxLength(50)]],
answers: ['', [Validators.required, proValidators.maxLength(2000)]],
directory: ['', [Validators.required]],
});
pro-validators.ts
import { FormControl } from '@angular/forms';
import Debug from '@core/services/debug';
import * as moment from 'moment';
function maxLength(n) {
return function (control: FormControl) {
const input = control.value,
e = { invalidMaxLength: true };
if (!input) {
return;
}
const len = input.toString().length;
if (len > n) {
return e;
}
};
}
export const proValidators = {
maxLength,
}