-
Notifications
You must be signed in to change notification settings - Fork 43
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
Be able to setup a field level validation schema #25
Comments
Hi! Sorry for the long wait. I broke my arm and am just recovering. So let's start with my thoughts on this:
felte/packages/validator-zod/src/index.ts Lines 14 to 32 in f5bf1c6
So instead of "extending" felte with the validator you may use that function directly inside of your Do you feel one of these suggestions work for you? Or is there a specific API you might have in mind? I am completely open to suggestions |
Move fast and break things shouldn't be taken so seriously. Sorry for your arm. It's interesting to know this "with or without JS" intention. With that in mind I do prefer to create an solution based on my app over that second suggestion's function and not grow the library API on a wrong direction. I will try that by tomorrow and I'll post it here how it ended being if this helps someone. Thanks! |
Currently trying to solve the same issue. Would be very helpful if you could share a multi-step form demo on a codesandbox if you find a solution you are happy with and feel like adheres to best practices. Thanks! |
Here is at REPL https://svelte.dev/repl/d83a688976fd466ab0a1d55400ab94f6?version=3.38.2 A bit simple but makes it, right? |
So, the problem of a multi-step validation could be taken easily. But the other part of this issue is about a method of transfering validations to a input directly, something like HTML does with <input required min=6 name='phone'/> but not possible if we want more complex validations. <input use:validate={...}/> This api would be against the library direction as it requires some changes that could be only used with js? Just asking, we can close the issue if is not the intention |
Hey all, after seeing #29 it seems that the most dev friendly way to solve for multi-step forms is to use a separate form per step. For a couple reasons:
If we do go towards the a separate form for each step route, would a helper to create multiple forms be useful at all? That seems to me like something trivial anyone could do if they actually need it on the project. Or having each step being a component with its own Is there a use-case that would be made too complex by using a separate form for each step? |
@outerlook The way I'd see this is that JS only "enhances" the experience. So most likely if you're planning on making your form work also without JS you would anyway need to add the attributes such as Using an action like you mentioned does raise a couple questions regarding implementation. Mainly if it would need to be implemented in the core package or if it's a niche enough use-case that can somehow be handled using an extender package (although this approach also raises some uncertainties). Another thing is that most of the issues we are finding have more to do with when Felte sets a field to "touched" rather than when/how it validates it. Validation occurs always, but the error won't be present on the "errors" store until the field is "touched", which currently happens on blur and on change per field, or on all fields when the form is submitted. |
So regarding multi-page forms I've posted a package to help with it, do not see this as a best practice yet since I am open for this to change a lot based on usage. There might be a lot of edge cases not considered. Seeing as the implementation is not so big this could eventually be merged to the core package. Although the current assumptions make it very simplistic. Feel free to test it. I've created a small example on CodeSandbox to showcase its usage. Do let me know of any edge cases you might find. |
Awesome! I had built this with my own custom store and counter for a specific use case, but your implementation looks more robust and I see you've already stubbed out Official author support goes without saying as a better path... Will port using your package, and post back if I find any edge cases or difficulties along the way. Thanks so much for jumping on these bug and feature requests so quickly. You're a one man army! |
Just following up. Your multi step package seems to work well for me so far, I'd suggest closing this out... If I come across some edge cases, I can put them in a new issue. Thanks again for the support! |
@ryanrussell I'm leaving this open as I am still exploring per-field validation to handle this as well. But do open a new issue if you find anything in that package! |
I'm sorry I could not reach up earlier. I am reading it all but right now we are delivering a 650 files migration to svelte 😅 I imagine I will be able to test and contribute with some thoughts in 1,5 week... |
@pablo-abc what you basically did here is to build a basic state-machine on top of If your specific usecase isn't covered by the builtin store-concept by svelte (which is used in the helper), use something like @xstate/svelte (btw @xstate/fsm is very lightweight, powerful and a perfect match for this kind of problems). So, imho there are two solutions to the multi-step/wizard-form-problem (and I have a clear preference):
Btw: here is the That said, there might be use-cases where field-level-validation is desirable (f.e. checking if a username is taken, pw-strength etc) and it would be a fair question if those are the responsibility of felte's validation-layer or if those should live in a customized input-component. I for myself lean towards the latter as it helps keeping bloat out of the nifty little core of felte :) |
Description
Hi! It would be nice to set some field level validation.
Example
My example is a multi-step form that shares the same
element. This form asks for phone, name and email.HTML
I use zod for validating things.
And I'm using the following schema:
### Schema
Example's issue
Every step is required. But I can't use like that. If all of them are required for submitting, I can't use the button as a
submit
button for controlling my form. And I do have some clear options:onSubmit
(a bit boilerplatey)There is an option I used on react, using the library react-hook-form
### Schema
### Creating form context
But it doesn't seems to work (it could be another issue if I did it right, but that's not the main request here)
Svelte-action
I've being trying to create an svelte action to solve that:
### Implementation
### Usage
But I'm having no success on that. It isn't preventing form
onSubmit
from being executed. I don't know how to achieve that outside felte.Further comments
The library I mentioned (react-hook-form) uses a different and less html native for subscribing new fields that can validate at this level.
RHF validation docs
I mention them only for bringing examples of libraries that have this kind of field-level API (and are choosing to continue supporting it).
Another benefit of solving it would be composability problem. A form would not need to know every field that are inside them for validating purposes.
The text was updated successfully, but these errors were encountered: