【问题标题】:Getting error in validation while submitting the form提交表单时出现验证错误
【发布时间】:2021-05-10 14:34:57
【问题描述】:

我有一个表格,在表格里面 我在选择下拉菜单时显示一个文本框 我正在调用一个表单数组,在那里我得到了多个文本框。 当我提交时,即使我为所有字段提供值,我的表单也显示无效。

  
           
                        
                        <input type="text" class="form-control" placeholder="Type" formControlName="type">                                      

                        <select class="Dropdown" formControlName="category" >
                            <option >Select Category</option>
                            <option  *ngFor="let list of dropdownitems" [value]="list.id" >{{list.name}}</option>
                        </select> 

                    <ng-container *ngIf="optionValue == '3'">
                                               
                          <input type="text" formControlName="listItem" class="form-control" placeholder="enter dropdownlist items"> 
                        <small class="errormessage" *ngIf="submitted && addForm.controls.listItem.hasError('required')">
                            Category is required 
                        </small>
                          <a (click)="addGroup()">Add New Textfield</a>

                    </ng-container>
                    
                     <span formArrayName="times" *ngFor="let time of addForm.controls.times?.value; let i = index;">                          
                      <span [formGroupName]="i">                         
                            <input type="text" class="form-control" formControlName="lists" placeholder="enter dropdown options">  

                      </span>              

在 ts 中,我正在验证所有字段。 onSubmit() 函数,如果表单无效,我将显示警报。 请更正我的代码并帮助我实现此功能。

 ngOnInit(): void {
    this.addForm = this.formBuilder.group({
      name: ['', [Validators.required, Validators.pattern('[a-zA-Z# ]*')]],
      times: this.formBuilder.array([])
      }); 
    console.log(this.addForm.value);
  }

public dropdownitems = [{
    name: "A",
    id: 1
  },{
    name: "B",
    id: 2
  },{
    name: "B",
    id: 3   
  },{
    name: "D",
    id: 4    
  }]

  onSubmit(){
    this.submitted = true;
    if (this.addForm.invalid) {
      alert ('Please fill up complete details');
      return;
    }
    console.log(this.addForm.value);   
}


value = this.formBuilder.group({
  lists: ['', Validators.required],
});


addGroup() {
  const val = this.formBuilder.group({
    lists: ['', Validators.required],
  });
  const form = this.addForm.get('times') as FormArray;
  form.push(val);
}

removeGroup(index) {
  const form = this.addForm.get('times') as FormArray;
  form.removeAt(index);
}

}

【问题讨论】:

    标签: angular forms validation


    【解决方案1】:

    问题在于,即使这些字段隐藏在模板中 (HTML),它们在表单中也被定义为 required

    您需要有条件地添加和删除表单字段,或者您需要将它们设为enabled / disabled 以忽略required 标志。

    分享 here 固定的 Stackblitz 以及可能的解决方案和小的改进(删除了 ngModel

    您在此线程上提出了类似的问题 form validation on hidden fields

    【讨论】:

    • 第一步,您了解您遇到问题的原因吗?
    • 检查这个答案,特别是stackoverflow.com/a/51379633/9019541,您需要根据您的选择启用或禁用
    • 是的,因为问题是您的值不是 3。是 C。您的下拉值是字母。尝试更改它以与 C 而不是 3 进行比较。
    • 您能否在 value.changes 中放置一个 console.log(category) 以打印出您收到的值?我正在尝试在手机上检查它,但 stackblitz 在手机上效果不佳
    • 好的。哪个数字?你有没有得到一个3?你也犯了另一个错误。使用响应式表单时,您不需要 [(ngModel)]。这也可能会扰乱对更改的检测并产生冲突。您正在混合使用两种方法:基于模板(使用 ngModel)和反应式表单(使用 formControlName)
    猜你喜欢
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 2014-02-25
    相关资源
    最近更新 更多