From ffb013930b3f68e7f1d7ea53e5eaa04a4d2ae94a Mon Sep 17 00:00:00 2001 From: Echo Date: Sun, 2 Mar 2025 09:43:09 -0600 Subject: [PATCH 1/4] validation --- frontend/src/lib/components/JoinProject.svelte | 2 ++ 1 file changed, 2 insertions(+) diff --git a/frontend/src/lib/components/JoinProject.svelte b/frontend/src/lib/components/JoinProject.svelte index 96004f9..5cad4b1 100644 --- a/frontend/src/lib/components/JoinProject.svelte +++ b/frontend/src/lib/components/JoinProject.svelte @@ -46,6 +46,8 @@
Join Code
Date: Sun, 2 Mar 2025 09:45:29 -0600 Subject: [PATCH 2/4] create validation --- .../src/lib/components/CreateProject.svelte | 89 +++++++++++++++++-- 1 file changed, 84 insertions(+), 5 deletions(-) diff --git a/frontend/src/lib/components/CreateProject.svelte b/frontend/src/lib/components/CreateProject.svelte index 924680a..40dc9f1 100644 --- a/frontend/src/lib/components/CreateProject.svelte +++ b/frontend/src/lib/components/CreateProject.svelte @@ -16,6 +16,13 @@ }); let events: Event[] = $state([]); let fetchedEvents = false; + let formErrors = $state({ + name: "", + description: "", + image_url: "", + event: "", + }); + let submitting = $state(false); async function fetchEvents() { try { @@ -29,13 +36,63 @@ } } - async function createProject() { + function validate() { + let isValid = true; + + formErrors = { + name: "", + description: "", + image_url: "", + event: "", + }; + + if (!project.name.trim()) { + formErrors.name = "Project name is required"; + isValid = false; + } + + if (!project.description.trim()) { + formErrors.description = "Project description is required"; + isValid = false; + } + + // img url check + if (project.image_url.trim()) { + const validExt = [".jpg", ".jpeg", ".png", ".gif", ".webp", ".svg"]; + const isValidExt = validExt.some((ext) => + project.image_url.toLowerCase().endsWith(ext), + ); + + if (!isValidExt) { + formErrors.image_url = + "Image URL must end with a valid image extension (.jpg, .jpeg, .png, .gif, .webp, .svg)"; + isValid = false; + } + } + + if (!project.event[0]) { + formErrors.event = "Please select an event"; + isValid = false; + } + + return isValid; + } + + async function createProject(e: Event) { + e.preventDefault(); + + if (!validate()) { + toast.error("Please fix the errors in the form"); + return; + } + try { + submitting = true; await ProjectsService.createProjectProjectsPost({ body: project, throwOnError: true, }); - toast("Project created successfully"); + toast.success("Project created successfully"); project = { name: "", readme: "https://example.com", @@ -48,12 +105,14 @@ }; } catch (err) { handleError(err); + } finally { + submitting = false; } }
-
+