Skip to content

Commit 81c20fd

Browse files
committed
Fixed immediate file validation.
More explicit assignment logic for file proxies.
1 parent 41b129f commit 81c20fd

File tree

3 files changed

+48
-30
lines changed

3 files changed

+48
-30
lines changed

src/lib/client/proxies.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,20 @@ export function fileProxy<T extends Record<string, unknown>, Path extends FormPa
167167
set(file: FileList | File | Nullable<T, Path> | Optional<T, Path>) {
168168
if (!browser) return;
169169

170-
if (!file || file instanceof File) {
170+
if (!file) {
171171
const dt = new DataTransfer();
172-
if (file) dt.items.add(file);
173-
else formFile.set(file as File);
174-
file = dt.files;
172+
fileProxy.set(dt.files);
173+
formFile.set(file as File);
174+
} else if (file instanceof File) {
175+
const dt = new DataTransfer();
176+
dt.items.add(file);
177+
fileProxy.set(dt.files);
178+
formFile.set(file);
179+
} else if (file instanceof FileList) {
180+
fileProxy.set(file);
181+
if (file.length > 0) formFile.set(file.item(0) as File);
182+
else formFile.set(initialValue as File);
175183
}
176-
177-
fileProxy.set(file);
178-
179-
if (file.length > 0) formFile.set(file.item(0) as File);
180-
else formFile.set(initialValue as File);
181184
},
182185
update() {
183186
throw new SuperFormError('You cannot update a fileProxy, only set it.');
@@ -201,7 +204,9 @@ export function filesProxy<
201204
T extends Record<string, unknown>,
202205
Path extends FormPathArrays<T, File[]>
203206
>(form: Writable<T> | SuperForm<T>, path: Path, options?: ProxyOptions) {
204-
const formFiles = fieldProxy(form, path as any, options) as FieldProxy<File[]>;
207+
const formFiles = fieldProxy(form, path as any, options) as FieldProxy<
208+
File[] | Nullable<T, Path> | Optional<T, Path>
209+
>;
205210
const filesProxy = writable<FileList>(browser ? new DataTransfer().files : ({} as FileList));
206211

207212
formFiles.subscribe((files) => {
@@ -225,25 +230,24 @@ export function filesProxy<
225230
},
226231
set(files: FileList | File[] | Nullable<T, Path> | Optional<T, Path>) {
227232
if (!browser) return;
233+
228234
if (!(files instanceof FileList)) {
229235
const dt = new DataTransfer();
230236
if (Array.isArray(files))
231237
files.forEach((file) => {
232238
if (file) dt.items.add(file);
233239
});
234-
else formFiles.set(files as File[]);
235-
files = dt.files;
236-
}
237-
238-
const newFiles = files;
239-
filesProxy.set(newFiles);
240-
241-
const output: File[] = [];
242-
for (let i = 0; i < newFiles.length; i++) {
243-
const file = newFiles.item(i);
244-
if (file) output.push(file);
240+
filesProxy.set(dt.files);
241+
formFiles.set(files);
242+
} else {
243+
const output: File[] = [];
244+
for (let i = 0; i < files.length; i++) {
245+
const file = files.item(i);
246+
if (file) output.push(file);
247+
}
248+
filesProxy.set(files);
249+
formFiles.set(output);
245250
}
246-
formFiles.set(output);
247251
},
248252
update(updater: Updater<File[] | Nullable<T, Path> | Optional<T, Path>>) {
249253
filesStore.set(updater(get(formFiles)));

src/lib/client/superForm.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,21 @@ export function superForm<
10581058
let NextChange: FullChangeEvent | null = null;
10591059

10601060
function NextChange_setHtmlEvent(event: FullChangeEvent) {
1061-
NextChange = event;
1061+
// For File inputs, if only paths are available, use that instead of replacing
1062+
// (fileProxy updates causes this)
1063+
if (
1064+
NextChange &&
1065+
event &&
1066+
Object.keys(event).length == 1 &&
1067+
event.paths?.length &&
1068+
NextChange.target &&
1069+
NextChange.target instanceof HTMLInputElement &&
1070+
NextChange.target.type.toLowerCase() == 'file'
1071+
) {
1072+
NextChange.paths = event.paths;
1073+
} else {
1074+
NextChange = event;
1075+
}
10621076
// Wait for on:input to provide additional information
10631077
setTimeout(() => {
10641078
Form_clientValidation(NextChange);

src/routes/(v2)/v2/files-proxy/+page.svelte

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
const { form, tainted, message, enhance, errors } = superform;
1616
1717
const files = filesFieldProxy(superform, 'images');
18-
const values = files.values;
18+
const { values, valueErrors } = files;
1919
2020
const file = fileProxy(form, 'image');
2121
@@ -111,13 +111,13 @@
111111
name="images"
112112
type="file"
113113
/>
114-
{#if Object.keys($errors.images ?? {}).length}
115-
<ul class="invalid">
116-
{#each Object.keys($errors.images ?? {}) as error, i}
114+
<ul class="invalid">
115+
{#each $valueErrors as error, i}
116+
{#if error}
117117
<li>Image {i + 1}: {error}</li>
118-
{/each}
119-
</ul>
120-
{/if}
118+
{/if}
119+
{/each}
120+
</ul>
121121
</label>
122122
<label>
123123
Upload one file, max 10 Kb: <input

0 commit comments

Comments
 (0)