简体   繁体   English

提交表单时,如果未选中复选框,我想显示错误消息?

[英]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.

相关问题 Angular Reactive Form - 如何验证单选按钮并在提交表单时显示错误消息 - Angular Reactive Form - How to validate radio buttons and show error message when form is submitted 当我单击隐藏和显示按钮时,提交了两次表单 - Two times form submitted, when I click the hide and show button 提交表单时清除验证消息 - Clear validation message when the form is submitted Angular Material:提交父表单时,必填的 mat-input 字段不显示错误 - Angular Material: A required mat-input field does not show error when the parent form is submitted Angular - 当表单无效时在提交时显示错误消息 - Angular - show error message on submit when form is invalid Angular - 选中复选框时隐藏/显示表格中的列 - Angular - hide/show column in a table when checkbox is selected 打开日期选择器角度材料时提交表格 - form is submitted when I open the datepicker angular material 我想将图例选项与复选框绑定(是否显示图例) - I want to bind the legend option with checkbox(show legend or not) 提交反应式表单后显示错误消息 - Show error message after reactive form submission 当表单中的值设置为true时,显示一个复选框按钮处于选中状态 - Showing a checkbox button as selected when value in form is set to true
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM