-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
161 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { Actions, PageServerLoad } from './$types.js'; | ||
|
||
import { superValidate, message } from '$lib/index.js'; | ||
import { zod } from '$lib/adapters/zod.js'; | ||
import { fail } from '@sveltejs/kit'; | ||
import { schema } from './schema.js'; | ||
|
||
export const load: PageServerLoad = async () => { | ||
return { | ||
form: await superValidate(zod(schema)) | ||
}; | ||
}; | ||
|
||
export const actions: Actions = { | ||
default: async ({ request }) => { | ||
const form = await superValidate(request, zod(schema)); | ||
console.log(form); | ||
|
||
if (!form.valid) return fail(400, { form }); | ||
|
||
return message(form, 'Form posted successfully!'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<script lang="ts"> | ||
import { page } from '$app/stores'; | ||
import { superForm } from '$lib/index.js'; | ||
import SuperDebug from '$lib/index.js'; | ||
export let data; | ||
const { form, errors, message, enhance } = superForm(data.form); | ||
</script> | ||
|
||
<SuperDebug data={$form} /> | ||
|
||
<h3>when using zod-i18n-map, field errors are [undefined]</h3> | ||
|
||
<p>Submit to see localized errors.</p> | ||
|
||
{#if $message} | ||
<!-- eslint-disable-next-line svelte/valid-compile --> | ||
<div class="status" class:error={$page.status >= 400} class:success={$page.status == 200}> | ||
{$message} | ||
</div> | ||
{/if} | ||
|
||
<form method="POST" use:enhance> | ||
<label> | ||
Name<br /> | ||
<input name="name" aria-invalid={$errors.name ? 'true' : undefined} bind:value={$form.name} /> | ||
{#if $errors.name}<span class="invalid">{$errors.name}</span>{/if} | ||
</label> | ||
|
||
<label> | ||
Email<br /> | ||
<input | ||
name="email" | ||
type="email" | ||
aria-invalid={$errors.email ? 'true' : undefined} | ||
bind:value={$form.email} | ||
/> | ||
{#if $errors.email}<span class="invalid">{$errors.email}</span>{/if} | ||
</label> | ||
|
||
<button>Submit</button> | ||
</form> | ||
|
||
<hr /> | ||
<p> | ||
💥 <a target="_blank" href="https://superforms.rocks">Created with Superforms for SvelteKit</a> 💥 | ||
</p> | ||
|
||
<style> | ||
.invalid { | ||
color: red; | ||
} | ||
.status { | ||
color: white; | ||
padding: 4px; | ||
padding-left: 8px; | ||
border-radius: 2px; | ||
font-weight: 500; | ||
} | ||
.status.success { | ||
background-color: seagreen; | ||
} | ||
.status.error { | ||
background-color: #ff2a02; | ||
} | ||
input { | ||
background-color: #ddd; | ||
} | ||
a { | ||
text-decoration: underline; | ||
} | ||
hr { | ||
margin-top: 4rem; | ||
} | ||
form { | ||
padding-top: 1rem; | ||
padding-bottom: 1rem; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import i18next from 'i18next'; | ||
import { z } from 'zod'; | ||
import { zodI18nMap } from 'zod-i18n-map'; | ||
import translation from 'zod-i18n-map/locales/es/zod.json'; | ||
|
||
i18next.init({ | ||
lng: 'es', | ||
resources: { | ||
es: { zod: translation } | ||
} | ||
}); | ||
z.setErrorMap(zodI18nMap); | ||
|
||
export const schema = z.object({ | ||
name: z.string().min(2), | ||
email: z.string().email() | ||
}); | ||
|
||
const data = schema.safeParse({ name: '', email: '' }); | ||
if (!data.success) { | ||
console.dir(data.error.flatten(), { depth: 10 }); //debug | ||
} |