2
2
import SourceButton from ' $lib/components/SourceButton.svelte' ;
3
3
import { alertController , IonPage } from ' ionic-svelte' ;
4
4
5
- import { accountSchema as schema } from ' ./account.interface' ;
6
- // import { enhance, getFormWritable, validateField } from './spa-enhance';
7
-
8
5
import { superForm , setMessage , setError } from ' sveltekit-superforms/client' ;
9
6
import { z } from ' zod' ;
10
7
11
- /*
12
- <h1>Edit user</h1>
13
-
14
- {#if $message}<h3>{$message}</h3>{/if}
15
-
16
- <form method="POST" use:enhance>
17
- <input type="hidden" name="id" bind:value={$form.id} />
18
-
19
- <label>
20
- Name<br />
21
- <input
22
- name="name"
23
- data-invalid={$errors.name}
24
- bind:value={$form.name}
25
- {...$constraints.name} />
26
- </label>
27
- {#if $errors.name}<span class="invalid">{$errors.name}</span>{/if}
28
-
29
- <label>
30
- E-mail<br />
31
- <input
32
- name="email"
33
- type="email"
34
- data-invalid={$errors.email}
35
- bind:value={$form.email}
36
- {...$constraints.email} />
37
- </label>
38
- {#if $errors.email}<span class="invalid">{$errors.email}</span>{/if}
39
-
40
- <button>Submit</button>
41
- </form>
42
-
43
- */
44
-
45
- // const form = getFormWritable();
46
-
47
8
const userSchema = z .object ({
48
9
firstName: z .string ().min (2 ).default (' ' ),
49
10
lastName: z .string ().min (2 ).default (' ' )
50
11
});
51
12
13
+ type User = z .infer <typeof userSchema >; // not used - but usefull
14
+
52
15
const { form, errors, message, constraints, enhance, delayed, validate } = superForm (
53
- { firstName: ' F ' , lastName: ' ss ' },
16
+ { firstName: ' ' , lastName: ' ' },
54
17
{
55
18
SPA: true ,
56
19
validators: userSchema ,
57
20
onUpdate(form ) {
58
21
console .log (' SUBMIT clicked, received form' , form );
59
22
},
60
23
onError({ result , message }) {
24
+ console .log (' ERROR received' , result , message );
61
25
message .set (result .error .message );
62
26
},
63
27
validationMethod: ' oninput'
64
28
}
65
29
);
66
30
67
- // $: console.log('Form received', $form);
68
-
69
31
function submit() {
70
32
if (! $errors ) {
71
33
const controller = alertController
96
58
}
97
59
}
98
60
99
- async function doStuff (e : any ) {
100
- console .log (' doStuff ' , e .detail .value , e .target .name );
101
-
102
- // const nameErrors = awai validate(e.target.value);
103
- // validate('firstName', { update: 'errors' }).then(console.log) ;
104
- // validate('lastName', { update: 'errors' }).then(console.log);
61
+ async function checkInput (e : any ) {
62
+ console .log (' Getting stuff ' , e .detail .value , e .target .name );
63
+ if ( e . target . name in $form ) {
64
+ // @ts-ignore
65
+ $form [ e . target . name ] = e . detail . value ;
66
+ }
105
67
const nameErrors = await validate (' firstName' , { update: false });
106
68
console .log (' validate errors' , nameErrors );
107
- /*
108
-
109
- const nameErrors = await validate('name', { update: false })
110
-
111
- // Validate and update field with a custom value
112
- validate('name', { value: 'Test' })
113
-
114
- // Validate a custom value, update errors only
115
- validate('name', { value: 'Test', update: 'errors' })
116
-
117
- // Validate and update nested data, and also taint the field
118
- validate(['tags', 1, 'name'], { value: 'Test', taint: true })
119
-
120
-
121
- */
122
69
}
123
70
</script >
124
71
@@ -144,30 +91,32 @@ validate(['tags', 1, 'name'], { value: 'Test', taint: true })
144
91
<ion-list lines =" full" class =" ion-no-margin ion-no-padding" >
145
92
<ion-item >
146
93
<ion-input
94
+ placeholder =" Start typing here to see superforms"
147
95
class:ion-invalid ={$errors .firstName }
148
96
class:ion-touched ={$errors .firstName }
149
- label =" First Name"
97
+ label =" First Name - Superforms supercharged "
150
98
label-placement =" stacked"
151
99
helper-text =" Here you may enter your first name - or something else"
152
100
error-text =" Please type more characters..."
153
101
name =" firstName"
154
102
type =" text"
155
103
value ={$form .firstName ?? ' ' }
156
- on:ionInput ={doStuff } />
104
+ on:ionInput ={checkInput } />
157
105
</ion-item >
158
106
159
107
<ion-item >
160
108
<ion-input
109
+ placeholder =" Start typing here to see superforms"
161
110
class:ion-invalid ={$errors .lastName }
162
111
class:ion-touched ={$errors .lastName }
163
- label =" Last Name"
112
+ label =" Last Name - Superforms supercharged "
164
113
label-placement =" stacked"
165
114
helper-text =" Here you may enter your last name - or something else"
166
115
error-text =" Please type more characters..."
167
116
name =" lastName"
168
117
type =" text"
169
118
value ={$form .lastName ?? ' ' }
170
- on:ionInput ={doStuff } />
119
+ on:ionInput ={checkInput } />
171
120
</ion-item >
172
121
173
122
<ion-item >
0 commit comments