From 6947d58c7c273efe7c11e68d530e7126e5c40771 Mon Sep 17 00:00:00 2001 From: Ryan Mwangi Date: Wed, 18 Dec 2024 14:51:48 +0300 Subject: [PATCH] feat(form): add form validation for name and description fields --- end2end/package-lock.json | 7 +++++++ end2end/package.json | 3 +++ src/components/item_form.rs | 8 ++++++++ 3 files changed, 18 insertions(+) diff --git a/end2end/package-lock.json b/end2end/package-lock.json index 3e0dc8a..8b63923 100644 --- a/end2end/package-lock.json +++ b/end2end/package-lock.json @@ -8,6 +8,9 @@ "name": "end2end", "version": "1.0.0", "license": "ISC", + "dependencies": { + "end2end": "file:" + }, "devDependencies": { "@playwright/test": "^1.44.1", "@types/node": "^20.12.12", @@ -40,6 +43,10 @@ "undici-types": "~5.26.4" } }, + "node_modules/end2end": { + "resolved": "", + "link": true + }, "node_modules/fsevents": { "version": "2.3.2", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", diff --git a/end2end/package.json b/end2end/package.json index a80ac59..da2e766 100644 --- a/end2end/package.json +++ b/end2end/package.json @@ -11,5 +11,8 @@ "@playwright/test": "^1.44.1", "@types/node": "^20.12.12", "typescript": "^5.4.5" + }, + "dependencies": { + "end2end": "file:" } } diff --git a/src/components/item_form.rs b/src/components/item_form.rs index 873352c..f9e4720 100644 --- a/src/components/item_form.rs +++ b/src/components/item_form.rs @@ -1,5 +1,6 @@ use leptos::*; use leptos_dom::ev::SubmitEvent; +use leptos::logging::log; #[component] pub fn ItemForm(on_submit: Box, String, u8)>) -> impl IntoView { @@ -21,6 +22,13 @@ pub fn ItemForm(on_submit: Box, Str let handle_submit = move |ev: SubmitEvent| { ev.prevent_default(); + + // Validation + if name.get().is_empty() || description.get().is_empty() || rating.get() < 1 || rating.get() > 5 { + log!("Validation failed: Check required fields."); + return; + } + on_submit( name.get(), description.get(),