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 { 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 = <TargetKey extends any>(key: TargetKey, values?: TranslationValues | undefined, formats?: Partial<Formats> | 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"),
});
}