Skip to content

Commit

Permalink
fix(file): parse empty files
Browse files Browse the repository at this point in the history
  • Loading branch information
ido-pluto committed Aug 16, 2024
1 parent fb63fd2 commit a88f4a6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
21 changes: 17 additions & 4 deletions packages/forms/src/components-control/form-utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ async function isBigFile(value: string) {
return;
}

if(data.failed){
if (data.failed) {
const chunksDir = path.join(tempDirectory, "chunks_" + data.id);
const errorMessage = path.join(chunksDir, "error.txt");
try {
Expand All @@ -161,7 +161,7 @@ async function isBigFile(value: string) {
}

export async function parseFiles(about: AboutFormName, astro: AstroGlobal, multiple: boolean, readonly: boolean) {
if(readonly) return;
if (readonly) return;

const { disposeFiles } = getContext(astro, '@astro-utils/forms');
let values = [about.formValue];
Expand All @@ -177,7 +177,7 @@ export async function parseFiles(about: AboutFormName, astro: AstroGlobal, multi
return;
}

if(typeof bigFile === "string"){
if (typeof bigFile === "string") {
hasFailed = true;
about.pushErrorManually('upload-failed', bigFile);
return;
Expand All @@ -192,7 +192,7 @@ export async function parseFiles(about: AboutFormName, astro: AstroGlobal, multi
} else {
const bigFile = await isBigFile(about.formValue);
if (bigFile) {
if(typeof bigFile === "string"){
if (typeof bigFile === "string") {
about.pushErrorManually('upload-failed', bigFile);
return;
}
Expand All @@ -209,3 +209,16 @@ export async function parseFiles(about: AboutFormName, astro: AstroGlobal, multi
}
}
}


export function parseEmptyFiles(about: AboutFormName, astro: AstroGlobal) {
if(astro.props.readonly) return;

if (about.formValue.size === 0) {
about.formValue = null;
}

if (astro.props.multiple) {
about.formValue = [];
}
}
5 changes: 4 additions & 1 deletion packages/forms/src/components-control/input-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getFormValue } from '../form-tools/post.js';
import AboutFormName from './form-utils/about-form-name.js';
import type HTMLInputRadioPlugin from './form-utils/bind-form-plugins/input-radio.js';
import { BindForm } from './form-utils/bind-form.js';
import { parseCheckbox, parseColor, parseDate, parseEmail, parseFiles, parseJSON, parseNumber, parseURL } from './form-utils/parse.js';
import { parseCheckbox, parseColor, parseDate, parseEmail, parseEmptyFiles, parseFiles, parseJSON, parseNumber, parseURL } from './form-utils/parse.js';
import { validateFunc, validateRequire, validateStringPatters } from './form-utils/validate.js';
import { getProperty } from 'dot-prop';
import { ZodType } from 'zod';
Expand Down Expand Up @@ -55,6 +55,9 @@ export async function validateFormInput(astro: AstroGlobal, bind: BindForm<any>,

// validate filed exits
if (!OK_INPUT_VALUE_NULL.includes(type) && !validateRequire(aboutInput, required)) {
if(type === 'file') {
parseEmptyFiles(aboutInput, astro);
}
aboutInput.setValue();
return;
}
Expand Down

0 comments on commit a88f4a6

Please sign in to comment.