简体   繁体   English

表单输入状态颜色不变

[英]Form input status color not changing

In one of my projects I have a scenario where I need to mark a field correct or incorrect depending on another field. 在我的一个项目中,我有一个场景,我需要根据另一个字段将一个字段标记为正确或不正确。 I have created an example to be able to ask a question like this here. 我创建了一个示例,可以在这里提出这样的问题。

The main problem: I'm unable to change the status color of an input by changing the value of ANOTHER input. 主要问题:我无法通过更改ANOTHER输入的值来更改输入的状态颜色。

For better understanding of the issue I will try to explain by images: Step one - I have written a bigger number in the Smaller Value input and blurred it and it shows an error which is expected: 为了更好地理解该问题,我将尝试通过图像进行解释:第一步-我在“较小值”输入中写了一个较大的数字,并对其进行了模糊处理,它显示了预期的错误:

在此处输入图片说明

Step two - I have written a bigger number in the Bigger Number input and blurred it, so I expect the SMaller Value input to be green, but it stays red: 第二步-我在“更大的数字”输入中写了一个更大的数字并将其模糊,因此我希望“较小值”输入为绿色,但仍保持红色:

在此处输入图片说明

What I'd like to achieve is: Whenever the smaller value is bigger, it's input is colored red, and whenever the smaller value is smaller, its input is colored green. 我想要实现的是:每当较小的值较大时,其输入就会显示为红色,而无论较小的值较小,其输入都将显示为绿色。

And it works when I change the value of Smaller Value and blur the input. 当我更改“较小值”的值并使输入模糊时,此方法有效。 However, for example, when I write a smaller number in the Bigger Number input and blur it, the Smaller Value input does not get colored red if it was green before. 但是,例如,当我在“较大数字”输入中写入较小的数字并将其模糊时,“较小值”输入如果之前为绿色,则不会变为红色。

This is my form and validator function: 这是我的表单和验证器功能:

   this.thisForm = new FormGroup({
      smallerValue: new FormControl('', [
        this.validateSuccessShortName.bind(this)
      ]),
      biggerNumber:new FormControl('', [
        Validators.required
      ])
    });
  }
  validateSuccessShortName(control: AbstractControl) {
      if (parseInt(control.value,10) > parseInt(this.biggerNumber, 10)) {
        return {value: control.value};
      }
      return null;
    }

I have written this function to attempt to trigger validation manually, and it does trigger the validation, it's just that the Bigger Number input is unable to change the validation status of the smaller input: 我编写了此函数以尝试手动触发验证,并且确实触发了验证,只是更大的数字输入无法更改较小输入的验证状态:

updateFields(){
  for (const field in this.thisForm.controls) {
  this.thisForm.controls[field].updateValueAndValidity();
}
}

Here is a STACKBLITZ that showcases the issue. 这是展示问题的STACKBLITZ

If you need any more details, please let me know! 如果您需要更多详细信息,请告诉我!

Crazy! 疯! The answer is to bind the built-in classes manually to the ion-items: 答案是将内置类手动绑定到离子项目:

<ion-item [class.ng-invalid]="!thisForm.controls.smallerValue.valid" [class.ng-valid]="thisForm.controls.smallerValue.valid">

Credit for this answer goes to brianlittmann in this github issue: 这个答案归功于github问题的brianlittmann:

https://github.com/ionic-team/ionic/issues/6040#issuecomment-315037781 https://github.com/ionic-team/ionic/issues/6040#issuecomment-315037781

Working stackblitz: https://stackblitz.com/edit/ionic-ctmumh?file=pages/home/home.html 工作中的stackblitz: https ://stackblitz.com/edit/ionic-ctmumh ? file = pages/home/home.html

There seems to be some kind of issue with ion-items and validations. 离子项目和验证似乎存在某种问题。 I had the same problem. 我有同样的问题。 Other's have asked too and as for Ionic 3 there was no response. 其他人也提出了要求,至于Ionic 3,则没有任何回应。

Similar issues: 类似问题:

https://github.com/ionic-team/ionic/issues/12344 https://github.com/ionic-team/ionic/issues/12344

https://github.com/ionic-team/ionic/issues/12102 https://github.com/ionic-team/ionic/issues/12102

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM