issue: isValid is false but errors is empty when using an invalid value (based on the validation) in defaultValues #10267
Replies: 8 comments 1 reply
-
honestly, this issue is from #10250 (comment) |
Beta Was this translation helpful? Give feedback.
-
This is an expected behavior of common form handling. For example, when opening a login form with field username and password, those fields are required by default but you will not see error message on them until you touched it and trigger validation. Could you please explain what the use case to show error message on form initialization ? If you really want to do this, just call |
Beta Was this translation helpful? Give feedback.
-
@leapful |
Beta Was this translation helpful? Give feedback.
-
This is by design as we implemented a validation strategy for a better user experience, users shouldn't have seen errors before they start to interact with inputs. If your app has a specific use case, then |
Beta Was this translation helpful? Give feedback.
-
Please use |
Beta Was this translation helpful? Give feedback.
-
Hi, @ilyanikulin, I have encountered the same "issue". In my case, we have a time range modal that, when opened, has default "Start" and "End" time values pre-selected. Nonetheless, if these conflict with any time ranges you have already added, an error should appear right away so that you understand why the "Add" button is disabled (since So we ended up doing this: const defaultValues = useMemo(
() => ({
start: currentTimeRange?.value?.start ?? DEFAULT_FORM_VALUES.start,
end: currentTimeRange?.value?.end ?? DEFAULT_FORM_VALUES.end,
}),
[currentTimeRange?.value?.start, currentTimeRange?.value?.end],
);
const {
control,
register,
handleSubmit,
getValues,
trigger,
reset,
formState: { errors, isSubmitting, isValid },
} = useForm<TimeRangeModalForm>({
mode: 'all',
defaultValues,
});
useEffect(() => {
reset(defaultValues);
}, [defaultValues, reset]);
/**
* Needed to trigger React Hook Form's validation for defaultValues,
* since by design the library does not trigger them for accessibility reasons
*/
useEffect(() => {
void trigger();
}, [trigger, isValid]);
return {
control,
errors,
register,
handleSubmit,
disabledSubmit: isSubmitting || !isValid,
};
} Hope this can be helpful to you! |
Beta Was this translation helpful? Give feedback.
-
hello everyone, |
Beta Was this translation helpful? Give feedback.
-
up |
Beta Was this translation helpful? Give feedback.
-
Version Number
7.43.9
Codesandbox/Expo snack
https://codesandbox.io/s/blazing-worker-5mpslw?file=/src/App.js:219-230
Steps to reproduce
Expected behaviour
isValid=false
errors={name: {...}}
What browsers are you seeing the problem on?
Chrome
Relevant log output
No response
Code of Conduct
Beta Was this translation helpful? Give feedback.
All reactions