H
Hono6mo ago
tcurdt

validating arrays coming from forms

The following code works
<form
action='?'
hx-delete='foo'
hx-target='#list'
>
<ul>
{state.foo.map(item => (
<li
id={`foo-${item.id}`}
key={item.id}>

{item.name} ({item.id})

<input type='checkbox' name="foo[]" value={item.id} />
</li>
))}
</ul>

<button type='submit' name='delete'>Delete Selected</button>
</form>


.delete('/foos', zValidator('form', z.object({
'foos[]': z.coerce.number().array(),
delete: z.string().optional()
})), async (c) => {
const { 'foos[]': foos } = c.req.valid('form')
<form
action='?'
hx-delete='foo'
hx-target='#list'
>
<ul>
{state.foo.map(item => (
<li
id={`foo-${item.id}`}
key={item.id}>

{item.name} ({item.id})

<input type='checkbox' name="foo[]" value={item.id} />
</li>
))}
</ul>

<button type='submit' name='delete'>Delete Selected</button>
</form>


.delete('/foos', zValidator('form', z.object({
'foos[]': z.coerce.number().array(),
delete: z.string().optional()
})), async (c) => {
const { 'foos[]': foos } = c.req.valid('form')
But it feels a little clunky. Is there a nicer way of doing this?
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server