Merge pull request 'Only allow to click submit button if validation is correct' (#67) from bug/SurveySubmit into main

Reviewed-on: #67
This commit is contained in:
cami 2021-06-26 23:26:16 +00:00
commit 431b3454bd

View file

@ -14,6 +14,7 @@ export default function Umfrage() {
const [skills, setSkills] = useState(""); const [skills, setSkills] = useState("");
let surveyIsValid = true; let surveyIsValid = true;
let errorMessage = ""
const onSubmitClick = (e) => { const onSubmitClick = (e) => {
e.preventDefault(); e.preventDefault();
@ -25,30 +26,33 @@ export default function Umfrage() {
}; };
if (!Number(age)) { if (!Number(age)) {
console.log("age is not a number"); errorMessage += "age is not a number\n"
surveyIsValid = false; surveyIsValid = false;
} }
if (gender === "DEFAULT" || gender === "") { if (gender === "DEFAULT" || gender === "") {
console.log("choose gender"); errorMessage += "choose gender\n"
surveyIsValid = false; surveyIsValid = false;
} }
if (education === "DEFAULT" || education === "") { if (education === "DEFAULT" || education === "") {
console.log("choose edu"); errorMessage += "choose edu\n"
surveyIsValid = false; surveyIsValid = false;
} }
if (skills === "DEFAULT" || skills === "") { if (skills === "DEFAULT" || skills === "") {
console.log("choose skills"); errorMessage += "choose skills\n"
surveyIsValid = false; surveyIsValid = false;
} }
if (surveyIsValid === true) { if (surveyIsValid === true) {
console.log(opts); console.log(opts);
}
authFetch("/umfrage", { authFetch("/umfrage", {
method: "post", method: "post",
body: JSON.stringify(opts), body: JSON.stringify(opts),
}); });
}
else {
console.log(errorMessage);
}
}; };
const handleAgeChange = (e) => { const handleAgeChange = (e) => {