You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I defined a yup.date.min()-validation rule for date to make sure that users cannot enter a date earlier than 1970-01. The rule runs on onChange-mode.
Now let's say a user wants to type the year-part of the date directly in the input and not via the date picker - eg. the year 1984 - he will type 1, 9, 8 and 4.
When typing 1, the year-part of the input shows 0001 and the .min-validation will throw an error as expected.
However in the next step, when typing 9 the date-input's value starts to behave in an unexpected manner and shows 0009 (and the error remains). This pattern continues when typing 8 and 4 which will lead to values of 0008 and 0004 respectively.
While trying to debug this problem, I tested a yup.string.min(5)-validation rule on the text input to see if I get a similar behaviour when the validation fails. However, this is not the case. I can still continue typing into the text field as expected also if the validation fails becasue I have less than five characters in the input
One other thing that I realized is that changing my form from <form onChange={handleSubmit(onSubmit)}> to <form onSubmit={handleSubmit(onSubmit)}> will solve the described issue with my date-input. However, I would like to keep my form submitting onChange if possible.
So what is wrong with my date-input? Any help in the right direction would be highly appreciated.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
With the help of react hook form and yup I created a simple form with two inputs called
date
(type="month"
) andtext
(type="string"
). Here is my codesandbox: https://codesandbox.io/s/react-hook-form-v7-validationschema-forked-myfm6m?file=/src/index.jsMy problem is the following:
I defined a
yup.date.min()
-validation rule fordate
to make sure that users cannot enter a date earlier than1970-01
. The rule runs ononChange
-mode.Now let's say a user wants to type the year-part of the
date
directly in the input and not via the date picker - eg. the year1984
- he will type1
,9
,8
and4
.When typing
1
, the year-part of the input shows0001
and the.min
-validation will throw an error as expected.However in the next step, when typing
9
thedate
-input's value starts to behave in an unexpected manner and shows0009
(and the error remains). This pattern continues when typing8
and4
which will lead to values of0008
and0004
respectively.While trying to debug this problem, I tested a
yup.string.min(5)
-validation rule on thetext
input to see if I get a similar behaviour when the validation fails. However, this is not the case. I can still continue typing into thetext
field as expected also if the validation fails becasue I have less than five characters in the inputOne other thing that I realized is that changing my form from
<form onChange={handleSubmit(onSubmit)}>
to<form onSubmit={handleSubmit(onSubmit)}>
will solve the described issue with my date-input. However, I would like to keep my form submittingonChange
if possible.So what is wrong with my
date
-input? Any help in the right direction would be highly appreciated.Beta Was this translation helpful? Give feedback.
All reactions