-
Hi everyone! I'm stuck on this one. I'm trying to implement a form that supports two actions import { zodResolver } from "@hookform/resolvers/zod";
import { ActionFunctionArgs } from "@remix-run/node";
import { Form } from "@remix-run/react";
import { getValidatedFormData, useRemixForm } from "remix-hook-form";
import zod from "zod";
import { Button } from "~/components/ui/button";
import { Input } from "~/components/ui/input";
const item = zod.object({
title: zod.string().max(255).optional(),
});
type Item = zod.infer<typeof item>;
const resolver = zodResolver(item);
export const action = async ({ request, params }: ActionFunctionArgs) => {
const { receivedValues, errors, data } = await getValidatedFormData<Item>(request, resolver);
// TODO: Implement a switch depending on the submit action
};
export default function Item() {
const { formState, handleSubmit, watch, setValue, register, getValues } = useRemixForm<Item>({
resolver,
defaultValues: {
title: "Hello",
},
});
return (
<Form method="post" action="." onSubmit={handleSubmit} className="space-y-4">
<Input {...register("title")} placeholder="Title" />
<Button type="submit" variant="ghost">
Delete
</Button>
<Button type="submit">Create</Button>
</Form>
);
} How would I go about solving this predicament? |
Beta Was this translation helpful? Give feedback.
Answered by
zanozbot
Jul 8, 2024
Replies: 1 comment
-
I ended up using the import { zodResolver } from "@hookform/resolvers/zod";
import { ActionFunctionArgs } from "@remix-run/node";
import { Form, useSubmit } from "@remix-run/react";
import { getValidatedFormData, useRemixForm } from "remix-hook-form";
import zod from "zod";
import { Button } from "~/components/ui/button";
import { Input } from "~/components/ui/input";
const item = zod.object({
title: zod.string().max(255).optional(),
});
type Item = zod.infer<typeof item>;
const resolver = zodResolver(item);
export const action = async ({ request, params }: ActionFunctionArgs) => {
if (request.method === "DELETE") {
// handle delete
return null;
}
const { receivedValues, errors, data } = await getValidatedFormData<Item>(request, resolver);
// ...
return null;
};
export default function Item() {
const submit = useSubmit();
const { formState, handleSubmit, watch, setValue, register, getValues } = useRemixForm<Item>({
resolver,
defaultValues: {
title: "Hello",
},
});
return (
<Form method="post" action="." onSubmit={handleSubmit} className="space-y-4">
<Input {...register("title")} placeholder="Title" />
<Button type="button" variant="ghost" onClick={() => submit(null, { method: "DELETE", action: "." })}>
Delete
</Button>
<Button type="submit">Create</Button>
</Form>
);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
zanozbot
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I ended up using the
useSubmit
hook. It's not the prettiest solution but it gets the job done.