gpt4 book ai didi

javascript - 如何使用 mat-checkbox 进行验证?

转载 作者:搜寻专家 更新时间:2023-10-30 21:48:11 24 4
gpt4 key购买 nike

我想知道如何检查复选框是否被选中,如果没有被选中,将向用户显示一条错误消息。我设法放置了错误消息,但无法将其与表格的其余部分一起使用。你能帮助我吗?这是我必须做的。

<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

关于javascript - 如何使用 mat-checkbox 进行验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50935481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com