允许基于反应形式的可用数量输入数量 [英] Allow Input Quantity Based On Available Quantity in Reactive Forms

查看:25
本文介绍了允许基于反应形式的可用数量输入数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need help in my problem since i need to input only the quantity based on the available quantity on my rows? How can i only submit the button if this requirement is met? How can i check for this? Here's the link 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]
    }))
  }

推荐答案

//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
      };
    }
  }
}

这篇关于允许基于反应形式的可用数量输入数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆