From ab3897bbc5e9f2ce71726817d080950946087006 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Mon, 10 Jun 2024 11:12:30 -0700 Subject: [PATCH] Add ed individual field validation for register page --- Client/src/Pages/Register/index.jsx | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Client/src/Pages/Register/index.jsx b/Client/src/Pages/Register/index.jsx index bd1b63760..6fb34de38 100644 --- a/Client/src/Pages/Register/index.jsx +++ b/Client/src/Pages/Register/index.jsx @@ -50,10 +50,21 @@ const Register = () => { return { ...acc, [err.path[0]]: err.message }; }, {}); setErrors(validationErrors); + } else { + setErrors({}); } }, []); const handleInput = (e) => { + const fieldName = idMap[e.target.id]; + // Extract and validate individual fields as input changes + const fieldSchema = registerValidation.extract(fieldName); + const { error } = fieldSchema.validate(e.target.value); + let errMsg = ""; + if (error) { + errMsg = error.message; + } + setErrors({ ...errors, [fieldName]: errMsg }); const newForm = { ...form, [idMap[e.target.id]]: e.target.value }; setForm(newForm); }; @@ -77,8 +88,7 @@ const Register = () => { } catch (error) { if (error.name === "ValidationError") { // TODO Handle validation errors - console.log(error); - alert("Invalid input"); + alert(error); } else if (error.response) { // TODO handle dispatch errors alert(error.response.msg);