Ash*_*aut 6 angular2-forms angular angular-reactive-forms
我有一个表单,它根据下拉列表中的某些值更改其验证。如果选择 A 大学,那么最低百分比要求为 60,如果选择 B 大学,则最低百分比会下降到 55。尽管我已经找到了一种方法来获取模板中的最小错误,这样我就不必费力了在我的模板中编写百分比值。虽然我不确定这是否是正确的方法。
<div formGroupName="marks">
<div class="form-group">
<label for="email" class="col-lg-3 control-label">Semester 1</label>
<div class="col-lg-3">
<input autocomplete="off"
#sem1
type="text"
min="0"
max="99"
maxlength="1"
maxlength="2"
class="form-control"
id="email"
placeholder="Sem 1 (%)"
(keypress)="onlyNumberKey($event)"
formControlName="sem1">
<span *ngIf="registrationForm.controls['marks'].controls['sem1'].hasError('required') &&
registrationForm.controls['marks'].controls['sem1'].touched"
class="text-danger">
Sem 1 percentage required
</span>
<span *ngIf="registrationForm.controls['marks'].controls['sem1'].hasError('min') ||
registrationForm.controls['marks'].controls['sem1'].hasError('max') ||
registrationForm.controls['marks'].controls['sem1'].hasError('minlength')||
registrationForm.controls['marks'].controls['sem1'].hasError('maxlength') &&
registrationForm.controls['marks'].controls['sem1'].touched"
class="text-danger">
Percenatge must be {{ registrationForm.get('marks.sem1')._errors.min.min }} and above.
</span>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
成分
registrationForm = new FormGroup({
college: new FormControl('', [
Validators.required, dropDrownValidator
]),
marks: new FormGroup({
sem1: new FormControl('',
[
Validators.required,
Validators.min(60),
Validators.max(99),
Validators.minLength(1),
Validators.maxLength(2)
]
)
})
});
Run Code Online (Sandbox Code Playgroud)
为了达到你想要的目的,你必须观察你的college 控制力的变化。
您可以使用模板来执行(change)此操作:
<select formControlName="college" (change)="handleChange($event.target.value)">
...
</select>
Run Code Online (Sandbox Code Playgroud)
甚至valueChanges在组件中使用:
this.registrationForm.get('college').valueChanges.subscribe(college => this.handleChange(college));
Run Code Online (Sandbox Code Playgroud)
在您的 中function,根据大学AbstractControl#setValidators用户的选择,您可以使用+ AbstractControl#updateValueAndValidity(应用)将最小值设置为验证器,如下所示:
handleChange(selectedCollege: string): void {
const min = selectedCollege === 'CollegeA' ? 60 : 55;
const control = this.registrationForm.get('marks.sem1');
control.setValidators([Validators.required, customMin(min)]);
control.updateValueAndValidity();
}
Run Code Online (Sandbox Code Playgroud)
尖端:
1 - 因为您使用的是模型驱动表单,所以您不应该将验证放入 HTML 中,保持干净并将所有内容放入组件中。
2 - 您可以使用FormBuilder以简化构建form(请参阅底部的演示)。
3 - 你永远不应该像这样访问私有属性:
{{ registrationForm.get('marks.sem1')._errors.min.min }}
Run Code Online (Sandbox Code Playgroud)
如果需要,errors只需使用即可。_errors
4 - 您可以进一步简化您的标记:
代替:
<span *ngIf="registrationForm.controls['marks'].controls['sem1'].hasError('required') &&
registrationForm.controls['marks'].controls['sem1'].touched"
class="text-danger">
Sem 1 percentage required
</span>
<span *ngIf="registrationForm.controls['marks'].controls['sem1'].hasError('min') ||
registrationForm.controls['marks'].controls['sem1'].hasError('max') ||
registrationForm.controls['marks'].controls['sem1'].hasError('minlength')||
registrationForm.controls['marks'].controls['sem1'].hasError('maxlength') &&
registrationForm.controls['marks'].controls['sem1'].touched"
class="text-danger">
Percenatge must be {{ registrationForm.get('marks.sem1')._errors.min.min }} and above.
</span>
Run Code Online (Sandbox Code Playgroud)
你可以有:
<div class="text-danger" *ngIf="registrationForm.get('marks.sem1').touched">
<span *ngIf="registrationForm.hasError('required', 'marks.sem1')">
Sem 1 percentage required
</span>
<span *ngIf="registrationForm.getError('customMin', 'marks.sem1') as error">
Percentage must be greater than or equal to {{ error.min }}.
</span>
</div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17003 次 |
| 最近记录: |