验证动态比较两个数字

huangapple go评论92阅读模式
英文:

yup validation comparing two number dynamically

问题

I'm trying to compare 2 number inputs with yup validation.

One of the names is number1 and the other is number2.
number2 must be greater than or equal to number1 and number1 must be less than or equal to number2.

If I enter a 5 to number1 and then I enter 4 to number 1 I am getting a yup validation error message as number2 must be greater than or equal to 5". It is okay.
But if I change number1 to 3 the error message still exists. Whereas in this case, number1 is less than number2.
How can I manage these two input error messages dynamically with yup validation?

My validation schema:

validationSchema: {
    number1: yup
        .number()
        .required()
        .when('number2', (number2: any, schema: any) => {
            if (number2) {
                return schema.max(number2);
            }
        }),
    number2: yup
        .number()
        .required()
        .when('number1', (number1: any, schema: any) => {
            if (number1) {
                return schema.min(number1);
            }
        }),
},

The error message I get
inputs with error message

I tried yup methods with using when or without when I also used ReactJs and React hook form.

I'm expecting that when I get an error message like the above if I change the other input all error messages disappear.

英文:

I'm trying to compare 2 number inputs with yup validation.

One of the names is number1 and the other is number2.
number2 must be greater than or equal to number1 and number1 must be less than or equal to number2.

If I enter a 5 to number1 and then I enter 4 to number 1 I am getting a yup validation error message as number2 must be greater than or equal to 5". It is okay.
But if I change number1 to 3 the error message still exists. Whereas in this case, number1 is less than number2.
How can I manage these two input error messages dynamically with yup validation?

My validation schema:

validationSchema: {
            number1: yup
                .number()
                .required()
                .when('number2', (number2: any, schema: any) => {
                    if (number2) {
                        return schema.max(number2);
                    }
                }),
            number2: yup
                .number()
                .required()
                .when('number1', (number1: any, schema: any) => {
                    if (number1) {
                        return schema.min(number1);
                    }
                }),
        },

The error message I get
inputs with error message

I tried yup methods with using when or without when I also used ReactJs and React hook form.

I'm expecting that when I get an error message like the above if I change the other input all error messages disappear.

答案1

得分: 0

解决方案


问题已经解决,但正如我之前所说,我正在使用 react-hook-form,问题与 react-hook-form 相关。我不得不为这两个输入的 Controller 添加 deps。

<Controller
  rules={{
    deps: ['input2']
  }}
/>

我找到解决方案的 GitHub 链接:https://github.com/orgs/react-hook-form/discussions/8633

英文:

Solution


The problem has been solved but as I said above I'm using react-hook-form and the problem was related to react-hook-form. I had to add deps to Controller of these two inputs.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;Controller
  rules={{
    deps: [&#39;input2&#39;]
  }}
/&gt;

<!-- end snippet -->

Github url that I found the solution: https://github.com/orgs/react-hook-form/discussions/8633

huangapple
  • 本文由 发表于 2023年7月13日 20:03:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定