在Angular反应形式中使用updateOn:'blur' [英] Use updateOn: 'blur' in Angular Reactive Forms
问题描述
仅当用户离开字段(onblur
)时,我才想开始验证表单的字段.
I would like to start the validation of the fields of my forms only when the user leave the field (onblur
).
我正在尝试这种方式:
this.profileForm = this.fb.group({
firstName: ['initial value',
[Validators.required, Validators.minLength(2), forbiddenNameValidator(/bob/i)],
[forbiddenNameValidatorAsync()],
{ updateOn: 'blur' }
],
lastName: ['b']
});
但是这种方式不起作用. 验证(同步和异步)是在onChange期间执行的.
But in this way it does not work. The validations (sync and async) are performed during onChange.
我还想问一下如何通过对lastName
进行检查来验证名称.
I would also like to ask how you can verify the name by making checks on the lastName
.
示例:如果姓名字段与姓氏相同(仅姓名字段,不是整个表单,不是两个字段),我希望禁用该字段.
Example: I would like the name field to be disabled if it is the same as the last name (only name field, not entire form not both fields).
推荐答案
将参数设置为 AbstractControlOptions
firstName: ['initial value',{
validators: [Validators.required, Validators.minLength(2), forbiddenNameValidator(/bob/i)],
asyncValidators: [forbiddenNameValidatorAsync()],
updateOn: 'blur'
}],
此处的示例 https://stackblitz.com/编辑/angular-hdna17?file=src%2Fapp%2Fhello.component.ts
这篇关于在Angular反应形式中使用updateOn:'blur'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!