Replies: 2 comments 3 replies
-
react hook form return errors based on the input name, I had a looked at your example. I find it a bit difficult to follow. I made a simple codesandbox to demonstrate: https://codesandbox.io/s/objective-tdd-pnbfzr?file=/src/App.js import { useForm } from "react-hook-form";
import Headers from "./Header";
import "./styles.css";
let renderCount = 0;
export default function App() {
const {
register,
handleSubmit,
formState: { errors }
} = useForm({
mode: "onChange",
resolver: async (data, context, options) => {
// do your logic here
console.log(data);
return {
errors: {
checkboxes: {
type: "test",
message: "this is incorrect"
}
}
};
}
});
const onSubmit = (data) => console.log(data);
renderCount++;
console.log(errors);
return (
<div>
<Headers
renderCount={renderCount}
description="Performant, flexible and extensible forms with easy-to-use validation."
/>
<form onSubmit={handleSubmit(onSubmit)}>
<input {...register("checkboxes")} value="1" type="checkbox" />
<input {...register("checkboxes")} value="2" type="checkbox" />
<input type="submit" />
</form>
</div>
);
} |
Beta Was this translation helpful? Give feedback.
1 reply
-
I've encountered the same problem! |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Version Number
7.34.0
Codesandbox/Expo snack
https://codesandbox.io/s/silly-wave-qzs303?file=/src/App.tsx
Steps to reproduce
errors returns from resolver {}
is logged in the consoleerrors returns from resolver {}
is logged in the consoleExpected behaviour
The error message "At least one field must be selected" should not be displayed, and the "errors returns from useFormState" JSON should be an empty object.
I think this is due to the logic that determines when field has been updated, which prevents clearing errors from fields that haven't been touched since they were marked as in an error state. This is a legitimate performance optimization, so I wouldn't be surprised if this is working as intended. However, I couldn't find another way to express validation that accounts for the values of multiple fields (in this example, requiring at least one checkbox to be checked).
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