Execute submit on every valid change (onSubmit when valid input after onChange) #7063
-
Maybe this is easy, but I'm new to RHF. I have a simple text input that I want to use to filter a list. What is the configuration needed to do this? I found this StackOverflow post, but I'm really unsatisfied with the answers, because I don't want to use Maybe I'm thinking in the wrong direction, but instead of handling that myself with Can anyone help? ...and the code for reference: import React from "react";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import * as z from "zod";
type InputProps = React.ComponentPropsWithoutRef<"input">;
export const Input = React.forwardRef<HTMLInputElement, InputProps>(
function Input(props, ref) {
return (
<input
type="text"
className="inline-block bg-blue-100 h-12 mt-10 shadow-sm hover:shadow-md rounded-md disabled:bg-gray-100 disabled:cursor-not-allowed"
{...props}
ref={ref}
/>
);
}
);
function Form(props: React.ComponentPropsWithoutRef<"form">): JSX.Element {
return (
<form className="w-full inline-block" {...props}>
{props.children}
</form>
);
}
const schema = z.object({
username: z.string().min(1, "name required")
});
type Schema = z.infer<typeof schema>;
export default function App() {
const {
register,
handleSubmit,
formState: { errors }
} = useForm<Schema>({
mode: "all",
reValidateMode: "onSubmit",
resolver: zodResolver(schema)
});
function onSubmit(data: Schema) {
console.log("Submitted!", data);
}
return (
<div className="App">
<h3>React Hook Form example</h3>
<Form onSubmit={handleSubmit(onSubmit)}>
<div>
<Input
placeholder="Username..."
id="username"
autoComplete="off"
{...register("username")}
/>
<p>{errors?.username && errors.username.message}</p>
</div>
</Form>
</div>
);
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 12 replies
-
watch(); // trigger each onChange
if (formState.isValid) {
onSubmit() // submit with render when form is valid
} |
Beta Was this translation helpful? Give feedback.
-
Not sure if this will solve your problem: https://codesandbox.io/s/quiet-pine-chfmz?file=/src/App.tsx import * as React from "react";
import { useForm } from "react-hook-form";
import Headers from "./Header";
import "./styles.css";
type FormValues = {
firstName: string;
};
let renderCount = 0;
export default function App() {
const {
register,
handleSubmit,
watch,
formState,
formState: { isValidating }
} = useForm<FormValues>({
mode: "onChange"
});
const onSubmit = (data: FormValues) => console.log(data);
renderCount++;
const data = watch();
React.useEffect(() => {
if (formState.isValid && !isValidating) {
console.log(data);
}
}, [formState, data, isValidating]);
return (
<div>
<Headers
renderCount={renderCount}
description="Performant, flexible and extensible forms with easy-to-use validation."
/>
<form onSubmit={handleSubmit(onSubmit)}>
<input
{...register("firstName", { required: true })}
placeholder="First Name"
/>
<input type="submit" />
</form>
</div>
);
} |
Beta Was this translation helpful? Give feedback.
-
Is there a reason why you have a form value firstName (as a string) and are trying to render it in a checkbox? I use boolean form values for that kind of thing. |
Beta Was this translation helpful? Give feedback.
Not sure if this will solve your problem: https://codesandbox.io/s/quiet-pine-chfmz?file=/src/App.tsx