Den*_*uka 1 validation checkbox angular angular6
在我的 Angular 6 应用程序注册表中,我有一个已选中的复选框。我需要检查用户是否已取消选中该复选框,如果是,我想显示一条错误消息。
这是我在 .html 文件中的复选框,
<div class="form-group">
<div class="round">
<input
type="checkbox"
id="checkbox33"
formControlName="agree">
<label for="checkbox33"></label>
</div>
<small class="text-muted">Agree to our <a href="#" class="link">Terms
Of Use</a> and <a href="#" class="link">Privacy Policy</a>.
</small>
<span *ngIf="!worldWideRegisterForm.get('agree').valid" class="text-danger">You should agree!</span>
</div>
Run Code Online (Sandbox Code Playgroud)
这是我在 .ts 文件中的 ngOnInit()
ngOnInit() {
this.worldWideRegisterForm = new FormGroup({
'userName': new FormGroup({
firstName: new FormControl(null, Validators.required),
lastName: new FormControl(null, Validators.required)
}),
'email': new FormControl(null, [Validators.required, Validators.email]),
'phone': new FormControl(null, Validators.required),
'address': new FormGroup({
line1: new FormControl(null, Validators.required),
line2: new FormControl(null),
}),
'area': new FormGroup({
'province': new FormControl(null, Validators.required),
'city': new FormControl(null, Validators.required),
'postalCode': new FormControl(null, Validators.required),
}),
'password': new FormControl(null, [Validators.required, Validators.minLength(6)]),
'agree': new FormControl(true, Validators.required)
});
}
Run Code Online (Sandbox Code Playgroud)
使用角度验证器 requiredTrue
'agree': new FormControl(true, Validators.requiredTrue)
Run Code Online (Sandbox Code Playgroud)
对于错误
<div *ngIf="myTrue.hasError('required')">
Agree the T&C
</div>
Run Code Online (Sandbox Code Playgroud)
官方链接https://angular.io/api/forms/Validators#requiredTrue
| 归档时间: |
|
| 查看次数: |
2148 次 |
| 最近记录: |