[英]How to do validation with mat-checkbox?
我想知道如何检查复选框是否被选中,如果没有被选中,将向用户显示一条错误消息。 我设法放置了错误消息,但无法将其与表单的 rest 一起使用。 你能帮助我吗? 这是我必须做的。
<mat-checkbox class="hcs-full-width" [formControl]="termsFormControl"> Aceite os termos de uso
</mat-checkbox>
<mat-error *ngIf="termsFormControl.hasError('required')">
Termo é
<strong>requirido</strong>
</mat-error>
.ts
export class AppComponent implements OnInit {
private subscription: Subscription;
uri: string;
ssid: string;
sessiondId: string;
ip: string;
mac: string;
ufi: string;
mgmtBaseUrl: string;
clientRedirectUrl: string;
req: string;
userName: string;
hmac: string;
name: string;
email: string;
constructor(@Inject(DOCUMENT) private document: any, private route: ActivatedRoute) {
}
ngOnInit() {
this.route.queryParams
.filter(params => params.mac)
.subscribe(params => {
console.log(params);
this.ssid = params.ssid;
this.sessiondId = params.sessionId;
this.ip = params.ip;
this.mac = params.mac;
this.ufi = params.ufi;
this.mgmtBaseUrl = params.mgmtBaseUrl;
this.clientRedirectUrl = params.clientRedirectUrl;
this.req = params.req;
this.hmac = params.hmac;
});
console.log("LOGAR: " + this.nameFormControl.valid);
console.log("LOGAR: " + this.termsFormControl.valid);
}
Logar() {
if (this.nameFormControl.valid && this.emailFormControl.valid && this.termsFormControl.valid) {
this.userName = this.name + ";" + this.email;
this.uri = "#" + this.ssid + "&sessionId=" + this.sessiondId + "&ip=" + this.ip + "&mac=" + this.mac + "&ufi=" + this.ufi + "&mgmtBaseUrl=" + this.mgmtBaseUrl + "&clientRedirectUrl=" + this.clientRedirectUrl + "&req=" + this.req + "&username=" + this.userName + "&hmac=" + this.hmac;
// console.log("LOGAR: " + this.nameFormControl.valid);
this.document.location.href = this.uri;
}
console.log("LOGAR: " + this.nameFormControl.valid);
console.log("LOGAR: " + this.termsFormControl.valid);
};
emailFormControl = new FormControl('', [
Validators.required,
Validators.email,
]);
nameFormControl = new FormControl('', [
Validators.required,
]);
termsFormControl = new FormControl('', [
Validators.required,
]);
}
使用内置验证器:Validators.requiredTrue
复选框不能那样工作,您必须实现自定义验证器并分配给您的termsFormControl
。 在您的情况下,它将是:
termsFormControl = new FormControl('', [(control) => {
return !control.value ? { 'required': true } : null;
}]
);
这里自定义验证器显式检查控件的值,如果为 false 则将required
错误设置为true
。 通过这种方式,您可以为表单设置状态并能够使用强大的功能。
请参阅 GitHub 上的以下问题以获取更多解释为什么必须实施自定义验证。
编辑 1
要在单击按钮时显示错误消息,您可以设置条件以在复选框无效和脏时引发错误:
<mat-error *ngIf="termsFormControl.invalid && termsFormControl.dirty">
Termo é <strong>requirido</strong>
</mat-error>
然后单击按钮,您可以将复选框设置为脏(我没有看到您的整个代码,但它应该是这样的):
onSubmit() {
this.form.get('termsFormControl ').markAsDirty();
}
编辑 2
基于验证Validators.requiredTrue
内置的@Julida 建议Validators.requiredTrue
可用于复选框元素。 它验证是否选中或取消选中复选框。 如果未选中,它将标记控制无效。
Required
在这种情况下不起作用,因为从技术上讲false
是一个值。 因此,您必须使用RequiredTrue
,例如:
termsFormControl = new FormControl(false, [Validators.requiredTrue]);
N.netheless,如果你需要检查这个选项是否已经被选中,比如说显示一条消息,你必须验证required
的错误:
get acceptTermsErrorMessage() {
if (this.form.hasError('required', 'termsFormControl')) {
return "You must accept the terms and conditions in order to continue;
}
return '';
}
<mat-checkbox
(change)="todoItemSelectionToggle($event)">{{node.item}}</mat-checkbox>
In .ts file if event.checked is true checkbox is selected else viceversa
todoItemSelectionToggle(event)
{
console.log = event.checked;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.