< script lang = "ts" context = "module" >
import { z } from "zod" ;
export const formSchema = z . object ({
type : z . enum ([ "all" , "mentions" , "none" ], {
required_error : "You need to select a notification type"
})
});
export type FormSchema = typeof formSchema ;
</ script >
< script lang = "ts" >
import { page } from "$app/stores" ;
import * as Form from "$lib/components/ui/form" ;
import * as RadioGroup from "$lib/components/ui/radio-group" ;
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 >> = $ page . data . radioGroup ;
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 method = "POST" action = "?/radioGroup" class = "w-2/3 space-y-6" use: enhance >
< Form.Fieldset {form} name = "type" class = "space-y-3" >
< Form.Legend > Notify me about... </ Form.Legend >
< RadioGroup.Root
bind: value ={$ formData . type }
class = "flex flex-col space-y-1"
>
< div class = "flex items-center space-x-3 space-y-0" >
< Form.Control let: attrs >
< RadioGroup.Item value = "all" { ... attrs} />
< Form.Label class = "font-normal" > All new messages </ Form.Label >
</ Form.Control >
</ div >
< div class = "flex items-center space-x-3 space-y-0" >
< Form.Control let: attrs >
< RadioGroup.Item value = "mentions" { ... attrs} />
< Form.Label class = "font-normal"
> Direction messages and mentions </ Form.Label
>
</ Form.Control >
</ div >
< div class = "flex items-center space-x-3 space-y-0" >
< Form.Control let: attrs >
< RadioGroup.Item value = "none" { ... attrs} />
< Form.Label class = "font-normal" > Nothing </ Form.Label >
</ Form.Control >
</ div >
< RadioGroup.Input name = "type" />
</ RadioGroup.Root >
< Form.FieldErrors />
</ Form.Fieldset >
< Form.Button > Submit </ Form.Button >
< SuperDebug data ={$ formData } />
</ form >
Copy < script lang = "ts" context = "module" >
import { z } from "zod" ;
export const formSchema = z . object ({
type : z . enum ([ "all" , "mentions" , "none" ], {
required_error : "You need to select a notification type"
})
});
export type FormSchema = typeof formSchema ;
</ script >
< script lang = "ts" >
import { page } from "$app/stores" ;
import * as Form from "$lib/components/ui/form" ;
import * as RadioGroup from "$lib/components/ui/radio-group" ;
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 >> = $ page . data . radioGroup ;
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 method = "POST" action = "?/radioGroup" class = "w-2/3 space-y-6" use: enhance >
< Form.Fieldset {form} name = "type" class = "space-y-3" >
< Form.Legend > Notify me about... </ Form.Legend >
< RadioGroup.Root
bind: value ={$ formData . type }
class = "flex flex-col space-y-1"
>
< div class = "flex items-center space-x-3 space-y-0" >
< Form.Control let: attrs >
< RadioGroup.Item value = "all" { ... attrs} />
< Form.Label class = "font-normal" > All new messages </ Form.Label >
</ Form.Control >
</ div >
< div class = "flex items-center space-x-3 space-y-0" >
< Form.Control let: attrs >
< RadioGroup.Item value = "mentions" { ... attrs} />
< Form.Label class = "font-normal"
> Direction messages and mentions </ Form.Label
>
</ Form.Control >
</ div >
< div class = "flex items-center space-x-3 space-y-0" >
< Form.Control let: attrs >
< RadioGroup.Item value = "none" { ... attrs} />
< Form.Label class = "font-normal" > Nothing </ Form.Label >
</ Form.Control >
</ div >
< RadioGroup.Input name = "type" />
</ RadioGroup.Root >
< Form.FieldErrors />
</ Form.Fieldset >
< Form.Button > Submit </ Form.Button >
< SuperDebug data ={$ formData } />
</ form >
Copy