How to setValue null to number input? #13067
Unanswered
narbek-abd
asked this question in
Q&A
Replies: 1 comment
-
solution: import * as V from "valibot";
import { useForm } from "react-hook-form";
import { valibotResolver } from "@hookform/resolvers/valibot";
// thanks to https://github.com/fabian-hiller/valibot/issues/1088
function initialNullable<TSchema extends V.GenericSchema>(schema: TSchema) {
return V.pipe(V.nullable(schema), schema);
}
export const schema = V.object({
numberRequired: initialNullable(V.pipe(V.number())),
});
export default function Page() {
const f = useForm<V.InferInput<typeof schema>>({
resolver: valibotResolver(schema),
defaultValues: {
numberRequired: null,
},
});
function setNull() {
f.setValue("numberRequired", null);
}
function onSubmit(body) {
const data = body as V.InferOutput<typeof schema>;
}
} |
Beta Was this translation helpful? Give feedback.
0 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.
-
if i make this filed V.nullable(), then it's no longer a required filed
Beta Was this translation helpful? Give feedback.
All reactions