0

我需要帮助解决我的问题,因为我只需要根据我的行上的可用数量输入数量吗?如果满足此要求,我如何才能提交按钮?我该如何检查这个?这是链接链接代码

initGroup() {
    let rows = this.addForm.get('rows') as FormArray;
    rows.push(this.fb.group({
      ingredient_id: ['', Validators.required],
      qty_available: new FormControl({ value: '', disabled: true }, Validators.required),
      qty: ['', Validators.required]
    }))
  }
4

1 回答 1

1
//when push the fbgroup, add a customValidator

rows.push(this.fb.group({
      ingredient_id: ['', Validators.required],
      qty_available: new FormControl({ value: '', disabled: true }, Validators.required),
      qty: ['', Validators.required]
    },{validator: this.customValidator('qty_available','qty')}
    ))

//the customValidator function can be in your app.component.ts
//It's work because the validator is "attached" to the FbGroup, not to the all form

customValidator(campo1:string,campo2:string) {
  return (group: FormGroup): {[key: string]: any} => {
    const available = group.controls[campo1];
    const qty=group.controls[campo2]
    if(available.value<qty.value){  //if error return "something" 
      return {
        out: true //<--THIS
      };
    }
  }
}
于 2018-01-03T09:47:13.250 回答