jaredpalmer / formik

Build forms in React, without the tears šŸ˜­
https://formik.org
Apache License 2.0
33.87k stars 2.79k forks source link

Optimizing validation for FastFields #1007

Open klis87 opened 5 years ago

klis87 commented 5 years ago

šŸš€ Feature request

Current Behavior

Currently FastField does rendering optimization, but still its onChange handler triggers validation for the whole validationSchema, which can be costly for really huge forms.

Desired Behavior

It would be perfect if onChange and onBlur of FastField could validate only slice of validationSchema

Suggested Solution

yup.reach is the answer - https://github.com/jquense/yup#yupreachschema-schema-path-string-value-object-context-object-schema Fast fields could call a new Formik method, which validates only schema corresponding to a given FastField. For instance, <FastField name="someName.nested" /> could do schema.reach('someName.nested') and use this for validation instead of validating the whole schema. Then, the validation result could be merger with Formik errors.

Who does this impact? Who is this for?

All users caring for the best possible form performance, who use Formik together with Yup, especially for big forms.

Describe alternatives you've considered

I have really big dynamic forms, sometimes +100 fields and I tried FastField - it is better now but for such a big form it is Yup which becomes a bottleneck - each key stroke validates 100+ fields, which could be especially costly for more expensive validators.

Alternatively I could use Field level validation, but I really like to keep validation logic only in yup, it is easy to test then and also it nicely keep good separation between yup and formik. Also, I dont like to repeat myself defining initialValue and schema.cast() does this automatically form me (provided I have all fields defined inside schema, not as field validators)

Additional context

N/A

koyta commented 5 years ago

Big +, because i have same problems, validating causing much performance issues in big forms

mschipperheyn commented 5 years ago

Why don't you set validateOnBlur and validateOnChange={false}?

klis87 commented 5 years ago

@mschipperheyn no, because we dont want to turn of validation, we still wanna validate a given fast field, but not the whole yup schema on each key stroke

stale[bot] commented 5 years ago

Hola! So here's the deal, between open source and my day job and life and what not, I have a lot to manage, so I use a GitHub bot to automate a few things here and there. This particular GitHub bot is going to mark this as stale because it has not had recent activity for a while. It will be closed if no further activity occurs in a few days. Do not take this personally--seriously--this is a completely automated action. If this is a mistake, just make a comment, DM me, send a carrier pidgeon, or a smoke signal.

klis87 commented 5 years ago

smoke signal

jglesner commented 4 years ago

+1. I had to do validateOnBlur and validateOnChange={false} to overcome this problem; however, validating only on blur negatively impacts the user experience because the user is required to click off or go to a different field. If there's an issue at that point, the user then needs to come back to the previous field and this is annoying to users. I would prefer for each field to only validate itself as @klis87 suggests so that (1) using Formik/Yup on large forms is performant, and (2) to provide the most real-time validation possible to users.