< script lang = "ts" context = "module" >
import { z } from "zod" ;
export const formSchema = z . object ({
username : z . string (). min (2). max (50)
});
export type FormSchema = typeof formSchema ;
</ script >
< script lang = "ts" >
import * as Form from "$lib/components/ui/form" ;
import { Input } from "$lib/components/ui/input" ;
import SuperDebug , {
type SuperValidated ,
type Infer ,
superForm
} from "sveltekit-superforms" ;
import { zodClient } from "sveltekit-superforms/adapters" ;
import { toast } from "svelte-sonner" ;
let data : SuperValidated < Infer < FormSchema >>;
export { data as form };
const form = superForm ( data , {
validators : zodClient ( formSchema ),
onUpdated : ({ form : f }) => {
if ( f . valid ) {
toast . success ( "You submitted" + JSON . stringify ( f . data , null, 2));
} else {
toast . error ( "Please fix the errors in the form." );
}
}
});
const { form : formData , enhance } = form ;
</ script >
< form action = "?/username" method = "POST" class = "w-2/3 space-y-6" use: enhance >
< Form.Field {form} name = "username" >
< Form.Control let: attrs >
< Form.Label > Username </ Form.Label >
< Input { ... attrs} bind: value ={$ formData . username } />
</ Form.Control >
< Form.Description > This is your public display name. </ Form.Description >
< Form.FieldErrors />
</ Form.Field >
< Form.Button > Submit </ Form.Button >
< SuperDebug data ={$ formData } />
</ form >
Copy < script lang = "ts" context = "module" >
import { z } from "zod" ;
export const formSchema = z . object ({
username : z . string (). min (2). max (50)
});
export type FormSchema = typeof formSchema ;
</ script >
< script lang = "ts" >
import * as Form from "$lib/components/ui/form" ;
import { Input } from "$lib/components/ui/input" ;
import SuperDebug , {
type SuperValidated ,
type Infer ,
superForm
} from "sveltekit-superforms" ;
import { zodClient } from "sveltekit-superforms/adapters" ;
import { toast } from "svelte-sonner" ;
let data : SuperValidated < Infer < FormSchema >>;
export { data as form };
const form = superForm ( data , {
validators : zodClient ( formSchema ),
onUpdated : ({ form : f }) => {
if ( f . valid ) {
toast . success ( "You submitted" + JSON . stringify ( f . data , null, 2));
} else {
toast . error ( "Please fix the errors in the form." );
}
}
});
const { form : formData , enhance } = form ;
</ script >
< form action = "?/username" method = "POST" class = "w-2/3 space-y-6" use: enhance >
< Form.Field {form} name = "username" >
< Form.Control let: attrs >
< Form.Label > Username </ Form.Label >
< Input { ... attrs} bind: value ={$ formData . username } />
</ Form.Control >
< Form.Description > This is your public display name. </ Form.Description >
< Form.FieldErrors />
</ Form.Field >
< Form.Button > Submit </ Form.Button >
< SuperDebug data ={$ formData } />
</ form >
Copy