Force submit
#11197
-
Is it possible to submit form but skip all validations? |
Beta Was this translation helpful? Give feedback.
Answered by
xkomiks
Nov 16, 2023
Replies: 3 comments
-
you can do something like this: import { useForm } from "react-hook-form";
export default function App() {
const { register, handleSubmit } = useForm();
const onSubmit = (data) => console.log(data);
return (
<form onSubmit={handleSubmit(onSubmit)}>
<label>Enter your age (Above 18): </label>
<input
defaultValue={19}
{...register("age", {
valueAsNumber: true,
validate: (age) => {
if (age <= 18) return true; // Here you can skip validation by returning true;
return age > 18; // here's our validation
}
})}
/>
<input type="submit" />
</form>
);
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Yes, it was worth adding context. I have a form with several steps and at some steps there are about 20 fields that are already wrapped in their own components with both register and |
Beta Was this translation helpful? Give feedback.
0 replies
-
My solution const onSubmit = (values) => {
...
}
const onSubmitHandler = useCallback((event) => {
methods.handleSubmit(onSubmit)(event);
}, [onSubmit , methods.handleSubmit]);
const forceSubmitWithoutValidation = useCallback(() => {
const values = methods.getValues();
void onSubmit(values);
}, [onSubmit, methods.getValues]);
return (
<form onSubmit={onSubmitHandler}>
...
<button>Submit</button>
<button type="button" onClick={forceSubmitWithoutValidation }>Submit without validation</button>
</form>
) |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
xkomiks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My solution