[英]when form is submitted i want to show error message if checkbox is not selected?
If no checkbox is selected I want to show error message.如果没有选中复选框,我想显示错误消息。 and hide error message when its checked.
并在检查时隐藏错误消息。 it is showing initially, when I deselect its not showing error message.Appreciate your help greatly.
它最初显示,当我取消选择它时不显示错误消息。非常感谢您的帮助。 Thank you.
谢谢你。
Here is my html code for three forms.这是我的三种形式的 html 代码。
<div *ngIf="submitted && fc.cb.errors" class="error-feedback error">
<p *ngIf="fc.cb.errors.required" class="text-danger">
Please select atleast one policy
</p>
</div>
</div>
<div class="form-check" *ngFor="let product of templateParamterTypes['cluster_network_policy']">
<label class="form-check-label text-break">
<input class="form-check-input form-control"
type="checkbox" [value]="true" formControlName="cb" [(ngModel)]="product.isChecked"
(change)="changeSelection(product.id)" /> {{ product.template_name }}
<span class="form-check-sign">
<span class="check"></span>
</span>
</label>
</div>
ts: ts:
this.clusterForm = this.formBuilder.group({
securityForm: this.formBuilder.group({
clusternames: [null, Validators.required],
label: [null, [Validators.required,Validators.pattern(/^[a-zA-Z0-9]+$/)]],
cb:[null, Validators.required],
productFormGroup: this.productFG
}),
changeSelection(id) {
this.checkedIDs = []
this.selectedItemsList = this.DisplayProductList.filter(product => product.isChecked);
this.selectedTypes = this.selectedItemsList.reduce((a, i) => {
if(a[i['policy_type']]){
a[i['policy_type']] = [...a[i['policy_type']], i];
}else{
a[i['policy_type']] = [i];
}
return a;
}, {});
this.checkedIDs = this.selectedItemsList.map(item => ({ id: item.id }));
const item = this.DisplayProductList.find(p => p.id === id);
if (item.isChecked) {
const fg: FormGroup = this.createFormGroup(item);
this.productFArray.push(fg);
return;
}
take one variable like this ...像这样取一个变量......
isCheckBoxSelected:boolean=false;
and make one method like this and put in (change) ...并制作这样的一种方法并放入(更改)...
checkboxChanged(){
this.isCheckBoxSelected=!this.isCheckBoxSelected;
}
and in HTML instead of below code并在 HTML 中而不是下面的代码
[value]="true"
use this code使用此代码
[value]="isCheckBoxSelected"
and change your if condition like this ...并像这样改变你的 if 条件......
*ngIf="submitted && !isCheckBoxSelected"
I passed an event in onclick method and added these lines.我在 onclick 方法中传递了一个事件并添加了这些行。 it worked.
有效。
changeSelection(id,e) {
if(!e.target.checked){
let ind = this.checkedIDs.findIndex(x =>x.id==id)
if(ind != -1){
this.checkedIDs.splice(ind,1)
}
}else{
this.isAtleastPolicyError = false;
}
submit method:提交方法:
if (this.clusterForm.invalid || this.checkedIDs.length == 0) {
this.isAtleastPolicyError = true;
return
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.