i18n enabled for bill form validation

This commit is contained in:
2024-02-16 22:25:09 +01:00
parent d30bd50e1a
commit 1da6479c80
2 changed files with 28 additions and 12 deletions

View File

@@ -7,6 +7,7 @@ import { ObjectId } from 'mongodb';
import { withUser } from '@/app/lib/auth'; import { withUser } from '@/app/lib/auth';
import { AuthenticatedUser } from '../types/next-auth'; import { AuthenticatedUser } from '../types/next-auth';
import { gotoHome } from './navigationActions'; import { gotoHome } from './navigationActions';
import { Formats, TranslationValues, useTranslations } from "next-intl";
export type State = { export type State = {
errors?: { errors?: {
@@ -18,9 +19,12 @@ export type State = {
message?:string | null; message?:string | null;
} }
const FormSchema = z.object({ type IntlTemplate = <TargetKey extends any>(key: TargetKey, values?: TranslationValues | undefined, formats?: Partial<Formats> | undefined) => string;
const FormSchema = (t:IntlTemplate) => z.object({
_id: z.string(), _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(), billNotes: z.string(),
payedAmount: z.string().nullable().transform((val, ctx) => { payedAmount: z.string().nullable().transform((val, ctx) => {
@@ -33,7 +37,7 @@ const FormSchema = z.object({
if (isNaN(parsed)) { if (isNaN(parsed)) {
ctx.addIssue({ ctx.addIssue({
code: z.ZodIssueCode.custom, code: z.ZodIssueCode.custom,
message: "Not a number", message: t("not-a-number"),
}); });
// This is a special symbol you can use to // This is a special symbol you can use to
@@ -46,7 +50,7 @@ const FormSchema = z.object({
if (parsed < 0) { if (parsed < 0) {
ctx.addIssue({ ctx.addIssue({
code: z.ZodIssueCode.custom, code: z.ZodIssueCode.custom,
message: "Value must be a positive number", message: t("negative-number")
}); });
// This is a special symbol you can use to // This is a special symbol you can use to
@@ -63,7 +67,7 @@ const FormSchema = z.object({
parseFloat parseFloat
const UpdateBill = FormSchema.omit({ _id: true }); const UpdateBill = ;
/** /**
* converts the file to a format stored in the database * converts the file to a format stored in the database
@@ -113,7 +117,12 @@ export const updateOrAddBill = withUser(async (user:AuthenticatedUser, locationI
const { id: userId } = user; const { id: userId } = user;
const validatedFields = UpdateBill.safeParse({ const t = useTranslations("bill-edit-form.validation");
// FormSchema
const validatedFields = UpdateBill(t)
.omit({ _id: true })
.safeParse({
billName: formData.get('billName'), billName: formData.get('billName'),
billNotes: formData.get('billNotes'), billNotes: formData.get('billNotes'),
payedAmount: formData.get('payedAmount'), payedAmount: formData.get('payedAmount'),
@@ -124,7 +133,7 @@ export const updateOrAddBill = withUser(async (user:AuthenticatedUser, locationI
console.log("updateBill.validation-error"); console.log("updateBill.validation-error");
return({ return({
errors: validatedFields.error.flatten().fieldErrors, errors: validatedFields.error.flatten().fieldErrors,
message: "Missing Fields. Field to Update Bill.", message: t("form-error-message"),
}); });
} }

View File

@@ -74,7 +74,14 @@
"notes-placeholder": "Notes", "notes-placeholder": "Notes",
"save-button": "Save", "save-button": "Save",
"cancel-button": "Cancel", "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": "location-delete-form":
{ {