angular - Setting dynamic validators on checkbox -
i have form consists of checkbox populated via json server. there 20 objects, , 5 of them contain selected = true, while others false.
my html:
<input formcontrolname='questioncontrol{{ question.question_id }}' #checkbox class="pvq-create-checkbox" type="checkbox" [name]="question.question_id" (change)="checkedstate($event, checkbox)" [checked]="ischecked(question)">
on initial population of form, setting ischecked(question) , based on selected remove validators controls selected=false. however, on click of checkbox setting validators with:
setvalidator(questionid) { //set min length validator this.answercontrolarr[questionid - 1].setvalidators(validators.minlength(4)); this.answercontrolarr[questionid - 1].setvalidators(validators.required); this.answercontrolarr[questionid - 1].updatevalueandvalidity(); console.log(this.answercontrolarr[questionid -1]); //set value // if(this.questionsarr[questionid - 1][3]){ // this.answercontrolarr[questionid - 1].setvalue(this.questionsarr[questionid - 1][3]); // } //check if value "" or not meet validator requirement // if(this.answercontrolarr[questionid - 1].value == null){ // this.answercontrolarr[questionid - 1].seterrors({"minlength": true}); // } if (this.answercontrolarr[questionid - 1].value == null || this.answercontrolarr[questionid - 1].value.length < 5) { this.answercontrolarr[questionid - 1].seterrors({ "minlength": true }); }
}
the issue is, set validators not work on newly selected checkbox. not set in terms of errors , validations. believe being overriden remove validators?
Comments
Post a Comment