Skip to content

Commit 18941c1

Browse files
committed
Added File Input Handling example
1 parent bd42902 commit 18941c1

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ _[demo/Full.svelte](https://github.com/cloudedots-projects/svelte-forms/blob/mas
7575
type FormData = {
7676
title: string,
7777
description: string,
78+
coverImage: FileList,
7879
users: {
7980
name: string,
8081
email: string,
@@ -101,12 +102,14 @@ _[demo/Full.svelte](https://github.com/cloudedots-projects/svelte-forms/blob/mas
101102
initialValues: {
102103
title: "", // Simple String
103104
description: "", // Simple String
105+
coverImage: "", // File Input
104106
users: [], // Complex Form Array
105107
},
106108
// Yup Validation Schema
107109
validationSchema: yup.object().shape({
108110
title: yup.string().min(8).required(),
109111
description: yup.string(),
112+
coverImage: yup.mixed().test(value => value?.length > 0), // Custom validation because yup does not suport file objects
110113
users: yup.array().of({
111114
name: yup.string().required(),
112115
email: yup.string().email().required(),
@@ -186,6 +189,27 @@ _[demo/Full.svelte](https://github.com/cloudedots-projects/svelte-forms/blob/mas
186189
{/each}
187190
{/if}
188191
192+
<input
193+
name="coverImage"
194+
accept="image/*"
195+
bind:files={$form.coverImage}
196+
use:formControl
197+
/>
198+
{#if $state.coverImage._errors?.length}
199+
{#each $state.coverImage._errors as error}
200+
<span class="error">{error}</span>
201+
{/each}
202+
{/if}
203+
204+
{#if $form.coverImage?.length}
205+
<div class="image-preview">
206+
<img
207+
src={URL.createObjectURL($form.coverImage[0])}
208+
alt="Cover"
209+
height="150" />
210+
</div>
211+
{/if}
212+
189213
{#each $form.users as user, index}
190214
<h2>
191215
User {user.name}

demo/Full.svelte

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
type FormData = {
88
title: string,
99
description: string,
10+
coverImage: string | FileList
1011
users: {
1112
name: string,
1213
email: string,
@@ -33,12 +34,14 @@
3334
initialValues: {
3435
title: "", // Simple String
3536
description: "", // Simple String
37+
coverImage: "", // File Input
3638
users: [], // Complex Form Array
3739
},
3840
// Yup Validation Schema
3941
validationSchema: yup.object().shape({
4042
title: yup.string().min(8).required(),
4143
description: yup.string(),
44+
coverImage: yup.mixed().test(value => value?.length > 0), // Custom validation because yup does not suport file objects
4245
users: yup.array().of(
4346
yup.object().shape({
4447
name: yup.string().required(),
@@ -119,6 +122,27 @@
119122
{/each}
120123
{/if}
121124

125+
<input
126+
name="coverImage"
127+
accept="image/*"
128+
bind:files={$form.coverImage}
129+
use:formControl
130+
/>
131+
{#if $state.coverImage._errors?.length}
132+
{#each $state.coverImage._errors as error}
133+
<span class="error">{error}</span>
134+
{/each}
135+
{/if}
136+
137+
{#if $form.coverImage?.length}
138+
<div class="image-preview">
139+
<img
140+
src={URL.createObjectURL($form.coverImage[0])}
141+
alt="Cover"
142+
height="150" />
143+
</div>
144+
{/if}
145+
122146
{#each $form.users as user, index}
123147
<h2>
124148
User {user.name}

0 commit comments

Comments
 (0)