Git Product home page Git Product logo

Comments (9)

fabian-hiller avatar fabian-hiller commented on June 29, 2024 2

The "problem" is related to the fact that Modular Forms uses Valibot only for validation and does not consider the output. The background is that with transform it is possible to make more extensive changes to the data set and Modular Forms does not know how to handle this. For example, if the data type changes, Modular Forms could break.

More infos:

For now, you can use Modular Forms's transform feature to trim a string: https://modularforms.dev/qwik/guides/transform-inputs

from modular-forms.

fabian-hiller avatar fabian-hiller commented on June 29, 2024 2

Thank you! Yes I will give some thought to this. Maybe it can make sense to change Modular Forms or to develop a form library with a similar API than Modular Forms that is perfectly tailored to Valibot.

from modular-forms.

maiieul avatar maiieul commented on June 29, 2024 1

Thanks for pointing to the related issues.

I'll try to create a similar function as proposed in #20.

The problem with using modular Form's transform is that it is not the DX I'm looking for. I want to define all my schema in one place and use it throughout my application. Using the transform feature of modular form, I will basically be rewriting the same code twice.

from modular-forms.

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

But I wonder if adding const parsedValues = parse(requestSchema, values) in my action isn't just the best API I can use right now.

Yes, this is the best workaround at the moment.

from modular-forms.

itsUndefined avatar itsUndefined commented on June 29, 2024 1

How about changing the implementation a bit to support the inference of the output of zod/valibot for the SubmitHandler? Of course it would mean that the validate option of createForm would also have to change to take into account the result of the schema parser. This is something that in my opinion would seriously level up the type safety of the form handling.

You are doing an awesome work already! Thank you for trying your best to create the best form library possible.

from modular-forms.

maiieul avatar maiieul commented on June 29, 2024 1

I don't think Modular Form's current API is so far from Zod/Valibot that it necessitates a new form library. I think the underlying issue here is mainly server-side; since applying transformations at the form level could yield to unexpected/buggy behavior client-side.

Server-side

With server actions, I think it makes sense to always apply validation (as is currently the case) and transformations (as is not currently the case) to respect what the schema says.

In my schema, I have

  firstName: () =>
    string([
      toTrimmed(),
    ]),

But toTrimmed doesn't get applied in my formAction$ unless I use my own parsed values with const parsedValues = parse(requestSchema, values) as explained above.

I expected this to work out of the box by applying toTrimmed with valiForm$ as the second argument to formAction$. Without that transformation getting applied, I will have strings like "blablabla " (with many spaces) in my database, which is something I definitely want to avoid.

So in

export const useFormAction = formAction$<LoginForm>((values) => {
  // Runs on server
}, valiForm$(LoginSchema));

The second argument with valiForm$(LoginSchema) should apply transformations out of the box, and even transformations that change the data type shouldn't be of issue here (right?). valiForm$ might not be easy to modify due to client-side requirements (see below 👇), so perhaps we could introduce a new valiParse$ function?

Client-side

On the client, I think that it doesn't always make sense to apply transformations client side.

For example, a toTrimmed transformation could prevent the user from adding multiple spaces at once client-side, and therefore tell him that it can't be done. On the other hand, what would transform(number(), (amount) => amount * 100) mean? Every time the user enters a number, the transformation would add two zeros at the end of it? That wouldn't mean anything.

So I think for client-side transformations, it's best to use Field-level transformations as in https://modularforms.dev/qwik/guides/transform-inputs.

from modular-forms.

itsUndefined avatar itsUndefined commented on June 29, 2024 1

Yes. You are correct that the schema level transformations only make sense on onSubmit$ and on formAction$.

from modular-forms.

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

Thank you for the detailed feedback. I agree in principle and will give it some thought.

from modular-forms.

maiieul avatar maiieul commented on June 29, 2024

But I wonder if adding const parsedValues = parse(requestSchema, values) in my action isn't just the best API I can use right now.

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.