本文介绍了在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 stackblitzCODE LINK

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

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

推荐答案

为更好地理解表单在此处检查.

使用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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 02:23