I'm trying to manually set errors, instead of letting my Reactive Form do it, I have a complex form that requires me to dynamically set and remove errors on dynamically generated controls.
– Taranjit KangJul 25 '17 at 20:09
1
The error only knows whether it failed the rule or not, it doesn't know what the parameters of the rule are. If the control needs to know what the length is supposed to be, you'll have to pass that in a different way.
– John MontgomeryJul 25 '17 at 20:14
https://angular.io/api/forms/ValidationErrors - says key:any - so why not minlength:4 ?? - param for https://angular.io/api/forms/AbstractControl#setErrors
– JGFMKJul 25 '17 at 21:22
Resolved my issue, I was not doing:
this.answerControlArr[questionId - 1].setValidators(Validators.compose([ Validators.required, Validators.minLength(4)]));
Instead I was declaring each separately like so:
this.answerControlArr[questionId - 1].setValidators( Validators.required);
this.answerControlArr[questionId - 1].setValidators( Validators.minLength(4));
Not realizing, It was overriding the first validator. Thank you guys very much.
– Taranjit KangJul 25 '17 at 21:28
I was gonna also say, maybe... updateValueAndValidity(opts:{onlySelf :true})
– JGFMKJul 25 '17 at 21:29
@TaranjitKang - Hah.. Like the examples.. https://angular.io/guide/form-validation#custom-validation-directive
– JGFMKJul 25 '17 at 21:32