Skip to content

Commit

Permalink
Fix advanced Valibot example
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonpittman committed Sep 11, 2023
1 parent 526d608 commit 6ed8997
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions packages/docs/src/routes/docs/(qwikcity)/action/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ The constructor of `valibot$` can also take a function that takes a `RequestEven
import { component$, useStylesScoped$ } from '@builder.io/qwik';
import { routeAction$, valibot$, Form } from '@builder.io/qwik-city';
import styles from './styles.css?inline';
import { string, minLength } from 'valibot';
import { string, object, minLength } from 'valibot';

export const useAddUser = routeAction$(
async (user) => {
Expand All @@ -360,10 +360,16 @@ export const useAddUser = routeAction$(
};
},
// Valibot schema is used to validate that the FormData includes "firstName" and "lastName"
valibot$({
firstName: string([minLength(1)]),
lastName: string([minLength(1)]),
})
valibot$((requestEvent) =>
object({
firstName: string([minLength(1)]),
// The last name is optional if the url contains the query parameter "lastname=optional"
lastName:
requestEvent.url.searchParams.get('lastname') === 'optional'
? string()
: string([minLength(1)]),
})
)
);

export default component$(() => {
Expand All @@ -372,38 +378,34 @@ export default component$(() => {

return (
<Form action={action} class="flow">
<label>
<span>First name:</span>
<input name="firstName" />
<p data-status="error">
{action.value?.failed && (
<span>{action.value.nested?.firstName}</span>
)}
</p>
</label>

<label>
<span>Last name:</span>
<input name="lastName" />
</label>
<label>
<span>First name:</span>
<input name="firstName" />
<p data-status="error">
{action.value?.failed && (
<span>{action.value.nested?.lastName}</span>
)}
{action.value?.failed && (
<span>{action.value.nested?.firstName}</span>
)}
</p>
</label>

<button type="submit">Add user</button>
<label>
<span>Last name:</span>
<input name="lastName" />
</label>
<p data-status="error">
{action.value?.failed && <span>{action.value.nested?.lastName}</span>}
</p>

{action.value?.success && (
<p data-status="success">
User {action.value?.firstName} added successfully.
</p>
)}
<button type="submit">Add user</button>

{action.value?.success && (
<p data-status="success">
User {action.value.firstName} added successfully.
</p>
)}
</Form>
);
});


````
</CodeSandbox>

Expand Down

0 comments on commit 6ed8997

Please sign in to comment.