清除表格后验证不起作用

时间:2019-10-18 11:25:53

标签: angular validation bootstrap-4 angular-reactive-forms

我无法使用this.stockForm.reset()清除;所以我用它来清除提交后的数据:

 clearall(): void {
        this.isEditMode = false;
        this.stockForm.reset({
            sku: '',
            productName: '',
            counted: '',
        });
        Object.keys(this.stockForm.controls).forEach(key => {
            this.stockForm.get(key).setErrors(null);
        });
}

当我再次单击提交按钮时,我无法获得验证;获取默认值;因此,请让我知道在清除表单后如何再次进行验证。

3 个答案:

答案 0 :(得分:0)

嗨,您只需要像这样休息一下表格即可:

 clearall(): void {
        this.isEditMode = false;
        this.stockForm.reset();
        Object.keys(this.stockForm.controls).forEach(key => {
            this.stockForm.get(key).setErrors(null);
        });
     }

答案 1 :(得分:0)

您可以在重置表单时尝试使用Validators.required吗?

clearall(): void {
        this.isEditMode = false;
        this.stockForm.reset({
            sku: ['', Validators.required],
            productName: ['', Validators.required],
            counted: ['', Validators.required],
        });
        Object.keys(this.stockForm.controls).forEach(key => {
            this.stockForm.get(key).setErrors(null);
        });
}

希望有帮助!干杯!

请检查此示例=> https://stackblitz.com/edit/angular-reactive-forms-lgguij

答案 2 :(得分:0)

重置表单后,您将通过调用setErrors(null)手动运行验证。 setErrors(null)表示您将form属性设置为有效,因此不会收到任何验证消息。

setErrors

删除以下代码后,它将开始工作

 Object.keys(this.stockForm.controls).forEach(key => {
        this.stockForm.get(key).setErrors(null);
    });
相关问题