-
-
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.
Client-side validation was ignored when a data property was missing o…
…r didn't match the schema type. Fixes #243.
- Loading branch information
Showing
8 changed files
with
143 additions
and
41 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
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
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
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,25 @@ | ||
import { message, superValidate } from '$lib/server'; | ||
import { schema } from './schema'; | ||
import { fail } from '@sveltejs/kit'; | ||
|
||
export const load = async () => { | ||
const form = await superValidate(schema); | ||
return { form }; | ||
}; | ||
|
||
export const actions = { | ||
default: async ({ request }) => { | ||
const formData = await request.formData(); | ||
console.log(formData); | ||
|
||
const form = await superValidate(formData, schema); | ||
console.log('POST', form); | ||
|
||
if (!form.valid) { | ||
form.message = 'Failed on server.'; | ||
return fail(400, { form }); | ||
} | ||
|
||
return message(form, 'Posted OK!'); | ||
} | ||
}; |
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,52 @@ | ||
<script lang="ts"> | ||
import { superForm } from '$lib/client'; | ||
import type { PageData } from './$types'; | ||
import SuperDebug from '$lib/client/SuperDebug.svelte'; | ||
export let data: PageData; | ||
// Missing a field, which will result in a console error. | ||
// @ts-ignore | ||
data.form.data = {}; | ||
const { form, errors, tainted, message, enhance } = superForm(data.form, { | ||
//dataType: 'json', | ||
validators: { | ||
age: (age) => | ||
isNaN(age) || age < 30 ? 'At least 30 years, please!' : null, | ||
name: (name) => | ||
name.length < 2 ? 'At least two characters, please!' : null | ||
}, | ||
taintedMessage: null | ||
}); | ||
</script> | ||
|
||
{#if $message}<h4>{$message}</h4>{/if} | ||
|
||
<form method="POST" use:enhance> | ||
<label> | ||
Age: <input name="age" type="number" bind:value={$form.age} /> | ||
{#if $errors.age}<span class="invalid">{$errors.age}</span>{/if} | ||
</label> | ||
<label> | ||
Name: <input name="name" bind:value={$form.name} /> | ||
{#if $errors.name}<span class="invalid">{$errors.name}</span>{/if} | ||
</label> | ||
<div> | ||
<button>Submit</button> | ||
</div> | ||
</form> | ||
|
||
<style lang="scss"> | ||
form { | ||
margin: 2rem 0; | ||
input { | ||
background-color: #dedede; | ||
} | ||
.invalid { | ||
color: crimson; | ||
} | ||
} | ||
</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,6 @@ | ||
import { z } from 'zod'; | ||
|
||
export const schema = z.object({ | ||
age: z.number().min(30), | ||
name: z.string().min(1) | ||
}); |