Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Valibot/i18n] Client side validation is not translated #190

Open
Ashyni opened this issue Mar 8, 2024 · 2 comments
Open

[Valibot/i18n] Client side validation is not translated #190

Ashyni opened this issue Mar 8, 2024 · 2 comments
Assignees
Labels
bug Something isn't working enhancement New feature or request

Comments

@Ashyni
Copy link

Ashyni commented Mar 8, 2024

Hello,

I know i18n is still new on valibot, i open this issue just to think about it.

Client side validation is not translated if globally set by v.setGlobalConfig({ lang: "en" }); and we currently can't pass options to valiForm() to set it locally.

Could be resolved by getting the global config in _parse() or use safeParse() which internally use _parse(input, getGlobalConfig(config));.

@fabian-hiller
Copy link
Owner

Thank you for creating this issue! You are right. The current implementation does not support i18n. I will improve this.

@fabian-hiller fabian-hiller self-assigned this Mar 9, 2024
@fabian-hiller fabian-hiller added bug Something isn't working enhancement New feature or request labels Mar 9, 2024
@fabian-hiller
Copy link
Owner

There is a small problem. Valibot is currently only a dev dependency of Modular Forms. Therefore I can only import types and not JavaScript functions like safeParse or getGlobalConfig. To be able to import it, I have to add Valibot as a peer dependency, but this would trigger a warning for any user using Modular Forms without Valibot.

The bigger problem is the current structure of Modular Forms packages. Normally I should release the adapters as a separate package. I plan to improve this when I rewrite the entire library, but that will take some time. For now, as a workaround, I recommend adding the updated valiForm adapter to your source code:

import type {
  FieldValues,
  ValidateForm,
  PartialValues,
  FormErrors,
} from '@modular-forms/solid';
import { type BaseSchema, type BaseSchemaAsync, safeParseAsync } from 'valibot';

/**
 * Creates a validation functions that parses the Valibot schema of a form.
 *
 * @param schema A Valibot schema.
 *
 * @returns A validation function.
 */
export function valiForm<TFieldValues extends FieldValues>(
  schema: BaseSchema<TFieldValues, any> | BaseSchemaAsync<TFieldValues, any>
): ValidateForm<TFieldValues> {
  return async (values: PartialValues<TFieldValues>) => {
    const result = await safeParseAsync(schema, values, {
      abortPipeEarly: true,
    });
    return result.issues
      ? result.issues.reduce<FormErrors<TFieldValues>>(
          (errors, issue) => ({
            ...errors,
            [issue.path!.map(({ key }) => key).join('.')]: issue.message,
          }),
          {}
        )
      : {};
  };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants