我试过使用一个空白项目,它可以工作。然而,在我正在处理的当前项目中,我有一堆问题会在输入字段上生成动态表单控件。而且我为 minlength = 4 设置了验证。问题是,当NgFor
使用这些值呈现表单时——如果该值不符合要求,则在我更改输入字段中的某些内容之前,内联验证不会触发。
另外,我注意到当我提交表单时,如果我不更改值,它们会显示为 null。但是当我进行更改时,它们会显示为已更改。被困在这个组件上几天了......我修复了一件事,另一件坏了。
<form [formGroup]="pvqForm" (ngSubmit)="onSubmit(pvqForm)" novalidate>
<div *ngFor="let question of questions | sortBy: 'selected'; let i = index" class="row container-generic">
<div class="col-md-8">
<div class="container-input-checkbox">
<label class="container-flex">
<input formControlName='questionControl{{ question.question_id }}' #checkBox class="pvq-create-checkbox" type="checkbox" [name]="question.question_id" (change)="checkedState($event, checkBox)" [checked]="isChecked(question)">
<div class="pvq-create-label">
<div *ngIf="lang == 'en'">
<p aria-label="English Question">{{ question.EN }}</p>
</div>
<div *ngIf="lang == 'fr'">
<p aria-label="French Question">{{ question.FR }}</p>
</div>
</div>
</label>
<label [@hideShow]="checkBox.checked ? 'show' : 'hide'">Answer
<input minlength=4 formControlName='answerControl{{ question.question_id }}' type="textbox" name="{{ question.question_id }}" [value]="question.answer ? question.answer : ''">
<div *ngIf="!pvqForm.controls['answerControl' + question.question_id ].valid" style="color: red;">
{{ "MINLENGTH" | translate }}
</div>
</label>
</div>
</div>
</div>
this.pvqForm = new FormGroup({
answerControl1: this.answer_1,
answerControl2: this.answer_2,
answerControl3: this.answer_3,
answerControl4: this.answer_4,
answerControl5: this.answer_5,
answerControl6: this.answer_6,
answerControl7: this.answer_7,
answerControl8: this.answer_8,
answerControl9: this.answer_9,
answerControl10: this.answer_10,
answerControl11: this.answer_11,
answerControl12: this.answer_12,
answerControl13: this.answer_13,
answerControl14: this.answer_14,
answerControl15: this.answer_15,
answerControl16: this.answer_16,
answerControl17: this.answer_17,
answerControl18: this.answer_18,
answerControl19: this.answer_19,
answerControl20: this.answer_20,
questionControl1: this.question_1,
questionControl2: this.question_2,
questionControl3: this.question_3,
questionControl4: this.question_4,
questionControl5: this.question_5,
questionControl6: this.question_6,
questionControl7: this.question_7,
questionControl8: this.question_8,
questionControl9: this.question_9,
questionControl10: this.question_10,
questionControl11: this.question_11,
questionControl12: this.question_12,
questionControl13: this.question_13,
questionControl14: this.question_14,
questionControl15: this.question_15,
questionControl16: this.question_16,
questionControl17: this.question_17,
questionControl18: this.question_18,
questionControl19: this.question_19,
questionControl20: this.question_20
});
checkedState(event, checkBox) {
let question_id = checkBox.name;
if (event.target.checked === true) {
if (this.counter < this.checkedLimit) {
//Sets validator for newly checked questions
// this.setValidator(question_id);
//Add the validator for the checkbox checked
this.counter++;
} else {
event.target.checked = false;
this.answerControlArr[question_id - 1].setValue(null);
// this.removeValidator(question_id);
}
} else if (this.counter > 0) {
//When clicking off the checkbox, set value to null
this.answerControlArr[question_id - 1].setValue(null);
// this.removeValidator(question_id);
this.counter--;
}
}
我还尝试设置验证器并根据检查状态动态删除它们,但没有运气。这导致表格无效。
setValidator(questionId) {
//Set Min length Validator
this.answerControlArr[questionId - 1].setValidators(Validators.minLength(4));
//Set the value
// if(this.questionsArr[questionId - 1][3]){
// this.answerControlArr[questionId - 1].setValue(this.questionsArr[questionId - 1][3]);
// }
//Check if Value is "" or does not meet the 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 });
}
}
removeValidator(questionId){
this.answerControlArr[questionId - 1].clearValidators();