FormArray响应式表单中设置值不小于0 [英] Set Value Not Less Than 0 In FormArray Reactive Forms

查看:26
本文介绍了FormArray响应式表单中设置值不小于0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地将quantityControl"formControlName 中输入字段中的值实现为不小于 1.但是我的问题是在 formArray 上时.如何将其设置为不小于 0 或不应为负数?

I have successfully implemented the value in the input field to not less than 1 in the "quantityControl" formControlName. However my problem is when on the formArray. How can i set that to not than less than 0 or should not be a negative number?

这是下面的代码和我的stackblitz的链接代码链接

Here's the code below and the link to my stackblitz CODE LINK

this.inquiryForm.get('quantityControl').valueChanges.pipe(
          filter(quantity => quantity < 1)
    ).subscribe(value => {
      console.log(value);

      this.inquiryForm.get('quantityControl').setValue(1);
    });

推荐答案

为了更好地理解 Form 点击此处.

For better understanding of Form check here.

使用 compose() 为您的输入字段配置多个自定义验证.

Use compose() to configure your Input field with multiple custom validations.

this.form = formBuilder.group({
        formControlNameValue:['', Validators.compose([Validators.required, positiveVal ])
        ]});

并实现positiveVal

and implement positiveVal

static positiveVal(control:Control):{ [key: string]: any; } {
  if (Number(control.value) < 0) {
    return {nonZero: true};
  } else {
    return null;
  }
}

这篇关于FormArray响应式表单中设置值不小于0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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