Replies: 1 comment 1 reply
-
I am also curious how people is approaching this as well. I see 3 possible solutions:
import { useForm } from '@conform-to/react';
import { parse } from '@conform-to/zod';
import { useTranslation } from "react-i18next";
import { Form } from '@remix-run/react';
import { json } from '@remix-run/node';
import { z } from 'zod';
import { authenticate } from '~/auth';
const schema = z.object({
email: z.string().min(1, 'email.required').email('email.invalid'),
password: z.string().min(1, 'password.required'),
});
export async function action({ params, request }: ActionArgs) {
// Nothing changed
}
export default function LoginForm() {
const lastSubmission = useActionData<typeof action>();
const { t } = useTranslation();
const [form, { email, password }] = useForm({
lastSubmission,
onValidate({ formData }) {
return parse(formData, { schema });
},
});
return (
<Form method="post" {...form.props}>
<div>
<label>Email</label>
<input type="email" name={email.name} />
<div>{t(email.error)}</div>
</div>
<div>
<label>Password</label>
<input type="password" name={password.name} />
<div>{t(password.error)}</div>
</div>
<button>Login</button>
</Form>
);
}
import { useForm } from '@conform-to/react';
import { parse } from '@conform-to/zod';
import { useTranslation } from "react-i18next";
import { Form } from '@remix-run/react';
import { json } from '@remix-run/node';
import { z } from 'zod';
import { authenticate } from '~/auth';
function createSchema(t) {
return z.object({
email: z.string().min(1, t('email.required')).email(t('email.invalid')),
password: z.string().min(1, t('password.required')),
});
}
export async function action({ params, request }: ActionArgs) {
const t = await i18n.getFixedT(request);
const formData = await request.formData();
const submission = parse(formData, {
schema: createSchema(t),
});
if (!submission.value || submission.intent !== 'submit') {
return json(submission, { status: 400 });
}
return await authenticate(submission.value);
}
export default function LoginForm() {
const lastSubmission = useActionData<typeof action>();
const t = useTranslation();
const [form, { email, password }] = useForm({
lastSubmission,
onValidate({ formData }) {
return parse(formData, { schema: createSchema(t) });
},
});
return (
<Form method="post" {...form.props}>
<div>
<label>Email</label>
<input type="email" name={email.name} />
<div>{email.error}</div>
</div>
<div>
<label>Password</label>
<input type="password" name={password.name} />
<div>{password.error}</div>
</div>
<button>Login</button>
</Form>
);
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I'm wondering if someone has already used this library in the context of i18n? How would you adapt the readme example with remix-i18next?
I see two solutions:
t()
withuseTranslation()
andgetFixedT()
respectively.t
function and returns a schema using thet
function (but I remember I already tried this technique on a large schema in the past, leading to VS code warming up my laptop..)Did anyone already come up with a clever alternative?
Beta Was this translation helpful? Give feedback.
All reactions