GithubHelp home page GithubHelp logo

Comments (2)

fabian-hiller avatar fabian-hiller commented on June 21, 2024 1

This behavior is intentional and probably expected in most cases. However, I agree that this should be configurable or we should switch to your approach in the long run.

For now, there is a workaround. You can copy the code from the Valibot adpater and set abortPipeEarly to false. You also need to change the logic so that fields with the same path are not overwritten, but the strings are concatenated.

from modular-forms.

Toeler avatar Toeler commented on June 21, 2024 1

Thank you. I didn't consider just rewriting valiForm. Indeed, that gets most of the way there although as you said, you would need to concatenate the errors rather than returning them as an array.

This is a hacked attempt as making use of domain knowledge that the string is actually a string[] | undefined.

import { $, implicit$FirstArg, type QRL } from '@builder.io/qwik';
import type {
	FieldValues,
	FormErrors,
	MaybeFunction,
	PartialValues,
	ValidateForm,
} from '@modular-forms/qwik';
import type { BaseSchema, BaseSchemaAsync } from 'valibot';

/**
 * See {@link valiFormWithErrors$}
 */
export function valiFormWithErrorsQrl<TFieldValues extends FieldValues>(
	schema: QRL<MaybeFunction<BaseSchema<TFieldValues, any> | BaseSchemaAsync<TFieldValues, any>>>,
): QRL<ValidateForm<TFieldValues>> {
	return $(async (values: PartialValues<TFieldValues>) => {
		const resolvedSchema = await schema.resolve();
		const result = await (typeof resolvedSchema === 'function'
			? resolvedSchema()
			: resolvedSchema
		)._parse(values, { abortPipeEarly: false });
		console.log('vali', result);
		return result.issues
			? result.issues.reduce<FormErrors<TFieldValues>>((errors, issue) => {
					const key = issue.path!.map(({ key }) => key).join('.') as keyof FormErrors<TFieldValues>;
					const fieldErrors: string[] = (errors[key] as unknown as string[] | undefined) ?? [];
					fieldErrors.push(issue.message);

					errors[key] = fieldErrors as unknown as string; // Intentionally hide an array as a string

					console.log(key, 'issues', errors);
					return errors;
			  }, {})
			: ({} as FormErrors<TFieldValues>);
	});
}

/**
 * Creates a validation functions that parses the Valibot schema of a form.
 *
 * @param schema A Valibot schema.
 *
 * @returns A validation function.
 */
export const valiFormWithErrors$ = implicit$FirstArg(valiFormWithErrorsQrl);

It works, but obviously isn't something worth proceeding with.

My use case is a user registration form with an Email and Password inputs.
The email field doesn't really matter, as that just has a single validator.
But the password field has a list of constraints, and under the field it shows these constraints and as the user types, each constraint ticks as it passes.

The two requirements is:

  1. Like #116 it would be good to validate each of these at different points (email on blur, but password on input)
  2. This issue, whereby a field reports all of the errors so that I can compare those against the initial constraints that I calculate (by running a validation against the empty input and getting all of the issues)

I will continue to watch this space for cleaner implementations, thanks.

from modular-forms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.