When an error occurs in a form, the text fields are cleared and the errors are set as shown below
switch(result){
case "SUCCESS":
// handle success case
case "ERROR1":
this.Form.controls.text1.setValue('');
this.Form.controls.text2.setValue('');
this.Form.controls.text1.setErrors({ 'error1': true });
break;
case "ERROR2":
this.Form.controls.text1.setValue('');
this.Form.controls.text2.setValue('');
this.Form.controls.text1.setErrors({ 'error2': true });
break;
case "ERROR3":
this.Form.controls.text1.setValue('');
this.Form.controls.text2.setValue('');
this.Form.controls.text1.setErrors({ 'error3': true });
break;
}
To avoid redundancy, a method was created as follows
setError(error : string){
this.Form.controls.text1.setValue('');
this.Form.controls.text2.setValue('');
this.Form.controls.text1.setErrors({ error: true });
}
switch(result){
case "SUCCESS":
// handle success case
case "ERROR1":
setError('error1');
break;
case "ERROR2":
setError('error2');
break;
case "ERROR3":
setError('error3');
break;
}
The fields are being cleared, but there is an issue with setting the errors. Any advice on how to correctly set a specific error as true?