英文:
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 -->
<Controller
rules={{
deps: ['input2']
}}
/>
<!-- end snippet -->
Github url that I found the solution: https://github.com/orgs/react-hook-form/discussions/8633
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论