I have one FormGroup, once the user is submitted and form has some error, I make submitted variable to true and shows the error, after that, if the user make any changes in the form the submitted variable should be false and hide the errors. Is there any way how I can do this? I am able to change the submitted variable to true, if the form has error and it is submitted, but if the user make any changes to the form after submittion, how I can set the submitted variable to false?
CodePudding user response:
inside your component's ngOnInit()
function, subscribe to the valueChanges
property of the FormGroup
.
so that each time the valueChanges
has a new emitted value you can reset the submitted
boolean property to false.
ngOnInit(): void {
this.userForm.valueChanges.subscribe(() => {
this.submitted = false;
});
}
CodePudding user response:
i think this can help you
component.html
<input type="text" id="planName" formControlName="plan_name" />
<span *ngIf="(newPlanForms.plan_name.touched || submitted) && newPlanForms.plan_name.errors?.required">
Plan name is required
</span>
component.ts
submitted = false;
newPlanForm = this.fb.group({
plan_name:new FormControl("",[Validators.required]),});
onSubmit(value:any){
this.submitted = true;
....
.....}