I have input radio in React Hook Form and I try to use it as real boolean, instead of string.
I know that RHF have valueAsNumber to convert it as number. I thought that setValueAs was a generic way to allow any conversion but I can't make it work.
const schema = z.object({
name: z.string().min(4, { message: "4 caractères minimum pour le name" }),
developer: z.boolean() // A real boolean is expected here
});
export default function App() {
const {
register,
handleSubmit,
watch,
formState: { errors }
} = useForm({
resolver: zodResolver(schema),
defaultValues: {
name: "John",
developer: false // default as boolean
}
});
return (
<form onSubmit={handleSubmit(onSubmit)} noValidate>
<input type="text" placeholder="Name" {...register("name")} />
<p>
Is developer ?
<label>
Oui
<input
{...register("developer", {
setValueAs: (val) => true // Try to make it as a real boolean but doesn't work
})}
type="radio"
value={true}
/>
</label>
<label>
Non
<input
{...register("developer", {
setValueAs: (val) => false
})}
type="radio"
value={false}
/>
</label>
</p>
<input type="submit" />
</form>
);
}
Any idea?