From 1da6479c80818877347b8da8660982d04e800fbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Dere=C5=BEi=C4=87?= Date: Fri, 16 Feb 2024 22:25:09 +0100 Subject: [PATCH] i18n enabled for bill form validation --- app/lib/actions/billActions.ts | 31 ++++++++++++++++++++----------- messages/en.json | 9 ++++++++- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/app/lib/actions/billActions.ts b/app/lib/actions/billActions.ts index 5370e4b..257cf07 100644 --- a/app/lib/actions/billActions.ts +++ b/app/lib/actions/billActions.ts @@ -7,6 +7,7 @@ import { ObjectId } from 'mongodb'; import { withUser } from '@/app/lib/auth'; import { AuthenticatedUser } from '../types/next-auth'; import { gotoHome } from './navigationActions'; +import { Formats, TranslationValues, useTranslations } from "next-intl"; export type State = { errors?: { @@ -18,9 +19,12 @@ export type State = { message?:string | null; } -const FormSchema = z.object({ +type IntlTemplate = (key: TargetKey, values?: TranslationValues | undefined, formats?: Partial | undefined) => string; + + +const FormSchema = (t:IntlTemplate) => z.object({ _id: z.string(), - billName: z.coerce.string().min(1, "Bill Name is required."), + billName: z.coerce.string().min(1, t("bill-name-required")), billNotes: z.string(), payedAmount: z.string().nullable().transform((val, ctx) => { @@ -33,7 +37,7 @@ const FormSchema = z.object({ if (isNaN(parsed)) { ctx.addIssue({ code: z.ZodIssueCode.custom, - message: "Not a number", + message: t("not-a-number"), }); // This is a special symbol you can use to @@ -46,7 +50,7 @@ const FormSchema = z.object({ if (parsed < 0) { ctx.addIssue({ code: z.ZodIssueCode.custom, - message: "Value must be a positive number", + message: t("negative-number") }); // This is a special symbol you can use to @@ -63,7 +67,7 @@ const FormSchema = z.object({ parseFloat -const UpdateBill = FormSchema.omit({ _id: true }); +const UpdateBill = ; /** * converts the file to a format stored in the database @@ -113,18 +117,23 @@ export const updateOrAddBill = withUser(async (user:AuthenticatedUser, locationI const { id: userId } = user; - const validatedFields = UpdateBill.safeParse({ - billName: formData.get('billName'), - billNotes: formData.get('billNotes'), - payedAmount: formData.get('payedAmount'), - }); + const t = useTranslations("bill-edit-form.validation"); + + // FormSchema + const validatedFields = UpdateBill(t) + .omit({ _id: true }) + .safeParse({ + billName: formData.get('billName'), + billNotes: formData.get('billNotes'), + payedAmount: formData.get('payedAmount'), + }); // If form validation fails, return errors early. Otherwise, continue... if(!validatedFields.success) { console.log("updateBill.validation-error"); return({ errors: validatedFields.error.flatten().fieldErrors, - message: "Missing Fields. Field to Update Bill.", + message: t("form-error-message"), }); } diff --git a/messages/en.json b/messages/en.json index 47efa3c..0652b45 100644 --- a/messages/en.json +++ b/messages/en.json @@ -74,7 +74,14 @@ "notes-placeholder": "Notes", "save-button": "Save", "cancel-button": "Cancel", - "delete-tooltip": "Delete bill" + "delete-tooltip": "Delete bill", + "validation": { + "bill-name-required": "Bill name is required", + "payed-amount-required": "Payed amount is required", + "not-a-number": "Not a number", + "negative-number": "Value must be a positive number", + "form-error-message": "Form validation error. Please check the form and try again." + } }, "location-delete-form": {