Replies: 2 comments 6 replies
-
You can make the async function action({ requset }) {
const formData = await request.formData();
// The name can be anything. `operation` is just an example
const operation = formData.get('operation');
// ... parse formData as usual
}
function Example() {
return (
<form>
{/* ... */}
<button name="operation" value="add">Add</button>
<button name="operation" value="edit">Edit</button>
<button name="operation" value="delete">Delete</button>
</form>
);
} |
Beta Was this translation helpful? Give feedback.
3 replies
-
Thinking about this again. It might be best using a discriminated union here as it would be possible to narrow down the type with a simple switch case. const schema = z.discriminatedUnion('type', [
z.object({
operation: z.literal('add'),
// ... add fields
}),
z.object({
operation: z.literal('edit'),
// ... edit fields
}),
z.object({
operation: z.literal('delete'),
// ... delete fields
}),
]),
async function action({ requset }) {
const formData = await request.formData();
const submission = parse(formData, { schema });
if (submission.intent !== 'submit' || !submission.value) {
return json(submission, { status: 400 });
}
switch (submission.value.operation) {
case 'add':
// ... add logic
break;
case 'edit':
// ...edit logic
break;
case 'delete':
// ... delete logic
break;
}
}
function Example() {
return (
<form>
{/* ... */}
<button name="operation" value="add">Add</button>
<button name="operation" value="edit">Edit</button>
<button name="operation" value="delete">Delete</button>
</form>
);
} |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, on the same route, I'm managing to add, update and delete, my schema validates if the name, type, currency, and id are not empty on add and edit, but on delete, i don't need that values, i need only the id to delete, how i manage different schemas?
schema:
Beta Was this translation helpful? Give feedback.
All reactions