i18n: internationalize required error

This commit is contained in:
Stavros
2025-07-15 02:15:17 +03:00
parent f25ab72747
commit 5854d973ea
4 changed files with 20 additions and 2 deletions

View File

@@ -12,6 +12,7 @@ import {
} from "../ui/form"; } from "../ui/form";
import { Button } from "../ui/button"; import { Button } from "../ui/button";
import { loginSchema, LoginSchema } from "@/schemas/login-schema"; import { loginSchema, LoginSchema } from "@/schemas/login-schema";
import z from "zod";
interface Props { interface Props {
onSubmit: (data: LoginSchema) => void; onSubmit: (data: LoginSchema) => void;
@@ -22,6 +23,11 @@ export const LoginForm = (props: Props) => {
const { onSubmit, loading } = props; const { onSubmit, loading } = props;
const { t } = useTranslation(); const { t } = useTranslation();
z.config({
customError: (iss) =>
iss.input === undefined ? t("fieldRequired") : t("invalidInput"),
});
const form = useForm<LoginSchema>({ const form = useForm<LoginSchema>({
resolver: zodResolver(loginSchema), resolver: zodResolver(loginSchema),
}); });

View File

@@ -8,6 +8,8 @@ import {
import { zodResolver } from "@hookform/resolvers/zod"; import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form"; import { useForm } from "react-hook-form";
import { totpSchema, TotpSchema } from "@/schemas/totp-schema"; import { totpSchema, TotpSchema } from "@/schemas/totp-schema";
import { useTranslation } from "react-i18next";
import z from "zod";
interface Props { interface Props {
formId: string; formId: string;
@@ -17,6 +19,12 @@ interface Props {
export const TotpForm = (props: Props) => { export const TotpForm = (props: Props) => {
const { formId, onSubmit, loading } = props; const { formId, onSubmit, loading } = props;
const { t } = useTranslation();
z.config({
customError: (iss) =>
iss.input === undefined ? t("fieldRequired") : t("invalidInput"),
});
const form = useForm<TotpSchema>({ const form = useForm<TotpSchema>({
resolver: zodResolver(totpSchema), resolver: zodResolver(totpSchema),

View File

@@ -51,5 +51,7 @@
"failedToFetchProvidersTitle": "Failed to load authentication providers. Please check your configuration.", "failedToFetchProvidersTitle": "Failed to load authentication providers. Please check your configuration.",
"errorTitle": "An error occurred", "errorTitle": "An error occurred",
"errorSubtitle": "An error occurred while trying to perform this action. Please check the console for more information.", "errorSubtitle": "An error occurred while trying to perform this action. Please check the console for more information.",
"forgotPasswordMessage": "You can reset your password by changing the `USERS` environment variable." "forgotPasswordMessage": "You can reset your password by changing the `USERS` environment variable.",
"fieldRequired": "This field is required",
"invalidInput": "Invalid input"
} }

View File

@@ -51,5 +51,7 @@
"failedToFetchProvidersTitle": "Failed to load authentication providers. Please check your configuration.", "failedToFetchProvidersTitle": "Failed to load authentication providers. Please check your configuration.",
"errorTitle": "An error occurred", "errorTitle": "An error occurred",
"errorSubtitle": "An error occurred while trying to perform this action. Please check the console for more information.", "errorSubtitle": "An error occurred while trying to perform this action. Please check the console for more information.",
"forgotPasswordMessage": "You can reset your password by changing the `USERS` environment variable." "forgotPasswordMessage": "You can reset your password by changing the `USERS` environment variable.",
"fieldRequired": "This field is required",
"invalidInput": "Invalid input"
} }