在formConfig的每个key对应的value中的验证方法部分填写验证规则代码,也可抽象成方法
以登录密码验证为例:
const _config = { id: [, , , {label: '', class: 'col-md-12 col-lg-12', hidden: true, type: 'static'}], code: [, , , {label: '员工编号', class: 'col-md-12 col-lg-12', type: 'static'}], name: [, , , {label: '员工姓名', class: 'col-md-12 col-lg-12', type: 'static'}], loginName: [, [Validators.required, Validators.maxLength(32)], , { label: '登录名', requiredError: '登录标识由6位以上字母、数字组成,可使用电子邮件地址', class: 'col-md-12 col-lg-12', }], bPassword: [, [Validators.required, Validators.minLength(8), this.confirmBPassword(), this.validPassWord()], , { label: '新密码', requiredError: '登录密码由6位以上字母、数字组成', type: 'password', class: 'col-md-12 col-lg-12', }], cPassword: [, [Validators.required, Validators.minLength(8), this.confirmCPassword(), this.validPassWord()], , { label: '确认密码', requiredError: '登录密码由6位以上字母、数字组成', type: 'password', class: 'col-md-12 col-lg-12', }] };
confirmCPassword(): ValidatorFn { return (control: AbstractControl): { [key: string]: any } => { return this.password !== control.value ? {'customValid': {error: '两次输入密码不一致'}} : null; }; } confirmBPassword(): ValidatorFn { return (control: AbstractControl): { [key: string]: any } => { this.password = control.value; return null; }; } validPassWord(): ValidatorFn { return (control: AbstractControl): {[key: string]: any} => { let result= /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]$/.test(control.value); return result ? null : {'customValid': {error: '密码格式为字母大小写加数字'}}; }; }