Skip to content

Commit

Permalink
Merge pull request #11 from local-mood/Hyeonmin
Browse files Browse the repository at this point in the history
Fix:회원가입 validate 로직 수정
  • Loading branch information
wokbjso authored Dec 21, 2023
2 parents 0916a37 + b32b3b8 commit aeca76f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
12 changes: 6 additions & 6 deletions src/common/utils/validateForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export function validateForm(formData: FormState): [Record<string, string>] {
const errorMessage: Record<string, string> = {};
const { id, password, name, check_password } = formData;

if (!name?.trim()) {
errorMessage.name = "이름을 입력해주세요.";
}

if (!id.trim()) {
errorMessage.id = "아이디를 입력해주세요.";
}
Expand All @@ -12,12 +16,8 @@ export function validateForm(formData: FormState): [Record<string, string>] {
errorMessage.password = "비밀번호를 입력해주세요.";
}

if (name && !name.trim()) {
errorMessage.name = "이름을 입력해주세요.";
}

if (check_password && !check_password.trim()) {
errorMessage.password = "비밀번호를 다시 입력해주세요.";
if (!check_password?.trim()) {
errorMessage.check_password = "비밀번호를 다시 입력해주세요.";
}

return [errorMessage];
Expand Down
9 changes: 5 additions & 4 deletions src/features/form/useForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FORM_TYPE } from "./constant/form-type";
import { FormState } from "./states/form-data-state";
import { validateForm } from "@/common/utils/validateForm";

const FIELD = ["id", "password", "name", "check_password"];
const FIELD = ["name", "id", "password", "check_password"];

export default function useForm({
type,
Expand Down Expand Up @@ -54,13 +54,14 @@ export default function useForm({
const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
setShowError(true);

if (!hasAnyError()) {
onSubmit(type === FORM_TYPE.LOGIN ? loginFormData : registerFormData);
} else {
for (const field of FIELD) {
alert(errorMessage[field]);
break;
if (errorMessage[field]) {
alert(errorMessage[field]);
break;
}
}
}
};
Expand Down

0 comments on commit aeca76f

Please sign in to comment.