Add ed individual field validation for register page

This commit is contained in:
Alex Holliday committed 2024-06-10 11:12:30 -07:00
1 parent 69c24223ab
commit ab3897bbc5
1 file changed
+12 -2
+12 -2
View File
@@ -50,10 +50,21 @@ const Register = () => {
return { ...acc, [err.path[0]]: err.message }; return { ...acc, [err.path[0]]: err.message };
}, {}); }, {});
setErrors(validationErrors); setErrors(validationErrors);
} else {
setErrors({});
} }
}, []); }, []);
const handleInput = (e) => { 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 }; const newForm = { ...form, [idMap[e.target.id]]: e.target.value };
setForm(newForm); setForm(newForm);
}; };
@@ -77,8 +88,7 @@ const Register = () => {
} catch (error) { } catch (error) {
if (error.name === "ValidationError") { if (error.name === "ValidationError") {
// TODO Handle validation errors // TODO Handle validation errors
console.log(error); alert(error);
alert("Invalid input");
} else if (error.response) { } else if (error.response) {
// TODO handle dispatch errors // TODO handle dispatch errors
alert(error.response.msg); alert(error.response.msg);