根据活动表格中的可用数量允许输入数量

问题描述 投票:0回答:1

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

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]
    }))
  }
angular angular-reactive-forms angular-forms reactive-forms
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
      };
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.