Validators.required 报错 {'required': true} 验证字段值是否为true Validators.requiredTrue {required: true} 邮箱 Validators.email {email: true} 最小长度 Validators.minLength(3) {minlength: {requiredLength: 3, actualLength: 2}} 还能够直接在input使用 <input minlength="5"> 最大长度 Validators.maxLength(5) {maxlength: {requiredLength: 5, actualLength: 7} <input maxlength="5"> 正则 Validators.pattern(/foo/) {pattern: {requiredPattern: '^[a-zA-Z ]*$', actualValue: '1'} <input pattern="[a-zA-Z ]*"> 这个页也能够直接在页面使用
//这个传参数的版本 export function maxLength(maxLength: number): ValidatorFn { return (control: AbstractControl): ValidationErrors|null => { return hasValidLength(control.value) && control.value.length > maxLength ? {'maxlength': {'requiredLength': maxLength, 'actualLength': control.value.length}} : null; }; } 若是不传参数的话就是这样的 这个本身编写的自定义的 export type ValidationErrors = { [key: string]: any }; export function (control: AbstractControl): ValidationErrors|null { return control.value.length > maxLength ? {'maxlength': {'requiredLength': maxLength, 'actualLength': control.value.length}} :null; }; }
这里能够参考源码中的写法数组
export interface Validator { /** 这个是直接的 */ validate(control: AbstractControl): ValidationErrors|null; /** 注册一个回调函数,当验证器输入改变时调用, 暂时没发现用处 */ registerOnValidatorChange?(fn: () => void): void; }
管道的自定义指令的时候异步
@Directive({ selector: '[customValidator]', // 注册指令,而后页面使用 providers: [{provide: NG_VALIDATORS, useExisting: CustomValidatorDirective, multi: true}] }) class CustomValidatorDirective implements Validator { validate(control: AbstractControl): ValidationErrors|null { return {'custom': true}; } }
禁用清空错误async
const arr = new FormArray([new FormControl()], () => ({'expected': true})); arr.errors // {'expected': true} arr.disable(); arr.errors // null arr.enable(); //解除禁用,就继续报错
可是当禁用的时候,从新给数组添加一个FormControl
也促发脏依赖更新,因此也会报错ide
fbForm: FormGroup; constructor( private fb: FormBuilder ) { this.fbForm = this.fb.group({ firstName: 'some value', login: ['some',[Validators.required]], //禁用 should: {value: 'should', disabled: true}, login:fb.control('',{updateOn:'blur'}), });
this.fbForm = this.fb.group({ firstName: 'some value', //第三个参数是异步校验器 login: ['some', null,[this.asyncValidator1, this.asyncValidator2]], arrOne: this.fb.array( ['one', 'two'], null, [this.asyncValidator1, this.asyncValidator2] ) } ); console.log(this.fbForm.get('arrOne')?.errors); console.log(this.fbForm.get('login')?.errors); // {'async1': true, 'async2': true}
若是第一个参数,那么值主要看value的值,第二个参数仍是校验 const control = new FormControl({ value: 'n/a', disabled: true }); const control = new FormControl('', Validators.required); const control = new FormControl('', { validators: Validators.required,// 校验器 asyncValidators: myAsyncValidator,// 异步校验器 updateOn: 'blur'// 校验方式 }); 事件更新 更新策略AbstractControl(表示控件自行更新的事件)。 离开的时候,才会触发校验 可能的值:'change'| 'blur'| 'submit' 默认值:'change' 咱们发现FormGroup, FormControl,FormArray 相似,第二个参数均可以这样写 this.fbForm = this.fb.group({ firstName: '', },{updateOn: 'blur',validators:this.addFn} ); const a = new FormArray([new FormControl(), new FormControl()], {updateOn: 'blur'}); const control = new FormControl('', { updateOn: 'blur' }); const control = new FormControl('', { updateOn: 'submit' }); ==== addFn(control: AbstractControl) { // 更改了,触发事件是失去焦点促发 if (control.dirty) { console.log(control.value); return {name1: true}; } } this.fbForm = this.fb.group({ firstName: ['some value', { updateOn: 'blur', validators: [this.addFn] }], three:fb.array([ 'one','two' ]) }) console.log(this.fbForm.get(['three',1])?.value);// get 还能这样查
setValidators const c = new FormControl(null); //单个值 c.setValidators(Validators.required); // 多个值 c.setValidators([Validators.minLength(5), Validators.required]); // 清空校验器 c.clearValidators(); //设置异步校验器 c.setAsyncValidators(asyncValidator('expected')); //多个异步校验器 c.setAsyncValidators([asyncValidator('expected')]);
异步校验器函数
export function asyncValidator(expected: string, timeouts = {}): AsyncValidatorFn { return (control: AbstractControl) => { const timeout = (timeouts as any)[control.value] ?? 0; const result = control.value != expected ? {async: true} : null; return createValidationPromise(result, timeout); }; } function createValidationPromise( result: ValidationErrors|null, timeout: number): Promise<ValidationErrors|null> { return new Promise(resolve => { if (timeout == 0) { resolve(result); } else { setTimeout(() => { resolve(result); }, timeout); } }); }
使用案例ui
function asyncFnOne(c:string): AsyncValidatorFn { return (control: AbstractControl)=> { console.log(control.value); return of({name1:true}) } } function asyncFnTwo(c:string): AsyncValidatorFn { return (control: AbstractControl)=> { console.log(control.value); return of({name2:true}) } } this.profileForm = new FormGroup({ firstName: new FormControl(1, null!, [asyncFnOne('xxx'),asyncFnTwo('bbb')]) }); this.profileForm.get('firstName').asyncValidator(new FormControl(11)) .subscribe(console.log); // {name1: true, name2: true}