问题描述
Am从动态JSON值创建自定义反应形式,试图隐藏用户在文本框中输入的字符以星号,
示例:假设用户正在输入“ hello world”。
预期输出:**********,当用户分别输入和 每个字符
还将进行模式验证,例如仅字母和数字,
我已经达到了上述的预期输出,但是验证无法按预期进行
编码我尝试过的内容
SetValidations(val: any) {
this.formBuilder.control(val.key);
this.newGeographyForm.addControl(
val.key.toString(),new FormControl('')
);
let validators = this.getValidatorFnArray(val.templateOptions)
debugger
this.newGeographyForm.get(val.key).setValidators(validators)
val.templateOptions.input_mask > 0 ?this.MaskValue(val.key,val.templateOptions.input_mask) : ''
}
MaskValue(formControlVal:any,maskType:any){
const regex = /\S/gi;
const subst = `*`;
this.newGeographyForm.get(formControlVal).valueChanges.pipe(distinctUntilChanged()).subscribe(fVal=>{
const result = fVal.replace(regex,subst);
this.newGeographyForm.get(formControlVal).patchValue( result,{emitEvent: false});
this.newGeographyForm.get(formControlVal).updateValueAndValidity();
})
}
getValidatorFnArray(obj): ValidatorFn[] {
const validators = [];
if (obj.is_mandatory) {
debugger
validators.push(Validators.required);
}
if (obj.min != null) {
debugger
validators.push(Validators.minLength(obj.min));
}
if (obj.max != null) {
validators.push(Validators.maxLength(obj.max));
}
if (obj.regex != null) {
validators.push(Validators.pattern(obj.regex));
}
return validators
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)