Comments (2)
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.
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:
- Like #116 it would be good to validate each of these at different points (email on blur, but password on input)
- 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)
- valiForm doesn't execute custom validation properly HOT 4
- hello facing types errors HOT 1
- Form submitted when filed has error HOT 2
- [Question] How to implement a controlled component contains multiple fields? HOT 3
- formAction$ code is not executed if Form component has "class" attribute HOT 8
- [Question] Render props pattern HOT 2
- Form action triggers drizzle polyfill issues. HOT 2
- Make the second parameter of the `formAction$` optional HOT 6
- loginForm.submitting not reactive
- Prevent Double Submit HOT 8
- Form action response is optional before form submit HOT 2
- Can I use modular-forms with action from "@solidjs/router"? HOT 11
- [React] Feature Request: Decouple Form and FormStore creation from components. HOT 2
- Form validation doesn't work when used with `valiForm` HOT 1
- Form shows as dirty when text input is focused but not changed.
- Zod Validation doesn't work with auto form. HOT 2
- [Solid] Is there a way to use field array with external store? HOT 1
- FieldValue does not accept bigint HOT 1
- Modularize the Form onSubmit method HOT 5
- Setting initial error state for a form field HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from modular-forms.