我正在从动态 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
}