我有一个简单的表格如下:
some.component.html
<form class="example-form" novalidate (ngSubmit)="onSubmit()" autocomplete="off" [formGroup]="testform">
<input type="text" formControlName="name" class="form-control" placeholder="Enter name" required/>
<app-show-errors [control]="claimform.controls.name"></app-show-errors>
<button type="submit" (click)="onSubmit()">Next</button>
</form>
some.component.ts
ngOnInit() {
this.testform= new FormGroup({
name: new FormControl('', { validators: Validators.required})
}, {updateOn: 'submit'});
}
onSubmit() {
if (this.testform.valid) {
alert('saving data');
} else {
this._validationService.validateAllFormFields(this.testform);
}
}
validationService.ts
validateAllFormFields(formGroup: FormGroup) {
Object.keys(formGroup.controls).forEach(field => {
const control = formGroup.get(field);
if (control instanceof FormControl) {
control.markAsTouched({ onlySelf: true });
} else if (control instanceof FormGroup) {
this.validateAllFormFields(control);
}
});
}
参考
问题
如果留空,表格将在提交时验证,但即使在我检查this.testform.valid
时填写值后它返回false。但是如果我在form
上删除了updateOn:'submit'
,那么它会在blur
输入控件上进行验证,当输入值时,它会验证表单 return true
。不确定updateOn
是否正常工作或我是否以适当的方式实现了这一点。有人能指出我正确的方向。
1 回答
在你的 HTML 中,你有两个调用
onSubmit()
函数,来自提交按钮:从形式:
第一个被触发的调用是按钮的触发器,它实际上没有更新你的被动形式,因为你将
FormGroup
的选项设置为{updateOn: 'submit'}
。要触发的第二个调用是表单的触发器,它执行实际的表单更新。这是
FormGroup
指令配置:正如我们在主办属性中看到的 DOM 表单的提交(通过在表单中聚焦或点击表单的提交按钮时按 ENTER 键触发)将调用onSubmit()函数:
然后将调用syncPendingControls()函数:
最后更新模型。
因此,在您的情况下,只需从提交按钮中删除
(click)="onSubmit()"
:此外,您在输入中不需要
required
DOM 元素属性,因为您使用 Reactive Forms APIvalidators: Validators.required
设置它,并且因为您将表单设置为novalidate
,这取消了 HTML5 表单验证。