0

I'm trying

control.setErrors({ minlength: true });

But how can you specify a min length of 4.

Taranjit Kang
  • 2,510
  • 3
  • 20
  • 40
  • I think you might be misunderstanding how `setErrors` works. What are you trying to do here? – John Montgomery Jul 25 '17 at 20:06
  • 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 Kang Jul 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 Montgomery Jul 25 '17 at 20:14
  • No, I have that already.. having issues with it thats why i'm resorting to this. – Taranjit Kang Jul 25 '17 at 20:28
  • https://angular.io/api/forms/ValidationErrors - says key:any - so why not minlength:4 ?? - param for https://angular.io/api/forms/AbstractControl#setErrors – JGFMK Jul 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 Kang Jul 25 '17 at 21:28
  • I was gonna also say, maybe... updateValueAndValidity(opts:{onlySelf :true}) – JGFMK Jul 25 '17 at 21:29
  • @TaranjitKang - Hah.. Like the examples.. https://angular.io/guide/form-validation#custom-validation-directive – JGFMK Jul 25 '17 at 21:32

0 Answers0