Comments (16)
For the use case: please see my first comment. The resolver may need access to additional data (aside from what is passed to it as arguments). The documentation does not seem to mention this limitation (only one validationResolver per component), therefore I reported it. IMO, it is needlessly limiting and breaks user expectations silently. Thanks for your efforts though, great lib.
from documentation.
related #192
from documentation.
The resolver may need access to additional data (aside from what is passed to it as arguments).
The reason why I introduced validationContext
, the lib itself cache validationResolver. Will validationContext
solve your problem? if so I will update the doc to reflect the caching mechanism on the website.
from documentation.
To solve my use case, I did roughly this:
export default function App() {
// create the context object in first render of this coemponent
// useMemo does not come with semantic guarantee, use lodash.memoize or similar
// see https://reactjs.org/docs/hooks-reference.html#usememo
const context = useMemo(() => ({}), [])
const date = new Date();
console.log("date in render", date);
// mutate the context object
context.date = date;
const { register, handleSubmit, errors } = useForm({
mode: "onBlur",
validationResolver,
validationContext: context
});
I..e. mutation of the memoized context object created during the first render of the component. While this may not be the best practice (mutated objects!), it works for me (until perhaps change is made to the hook to prevent memoizing the passed validationContext).
from documentation.
I will update the doc on this part:
validationResolver: will be cached inside react-hook-form, because you want to cache the validation rules instead change during render, same as validation schema.
validationContext: is not cached and you can change then as your component gets re-rendered.
let me know if above makes sense.
from documentation.
hey @rlaffers i have fixed the cache issue with validationContext, will be released in the next patch.
from documentation.
This should patch this issue: https://codesandbox.io/s/react-hook-form-validationresolver-cq2rh it's due to validationContext
is not memorized.
from documentation.
Thanks for your reply. Indeed, when the validationResolver is memoized (its identity does not change in between renders), the issue does not exist. However, your suggested change does not fix the actual problem.
Having the validationResolver change its identity in between renders is a valid use case I think. Let me provide some background. In my case, I need to access some additional data in my validation resolver. Standard way of doing this in JS is creating a closure over those data.
const createResolver = options => data => {
// do validation of data using those options
}
export default function App() {
const options = {} // this is a dynamic object. Think some external state. Cannot be memoized and can change on each render.
const resolver = createResolver(options)
const { register, handleSubmit, errors } = useForm({
mode: "onBlur",
validationResolver: resolver,
});
// trimmed the rest
}
In short, when a new validationResolver function is created and passed to useForm during subsequent renders, it breaks validation.
from documentation.
can you use validationContext? take a look my codesandbox above.
from documentation.
Yes, I looked at it but it bypasses the actual issue which I reported. In your codesandbox, there is only a single validationResolver function:
const validationResolver = resolver;
When I changed it so that there is a different validationResolver on each render, it fails:
const validationResolver = resolver.bind(null)
https://codesandbox.io/s/react-hook-form-validationresolver-tiyde
from documentation.
what's the use case for different validationResolver
for a single form?
FYI: we are not supporting that type of usage at the moment.
from documentation.
I see your point. Passing those data with validationContext would solve my problem if validationContext was not memoized inside the hook.
Please see:
https://codesandbox.io/s/react-hook-form-validationresolver-640tr
The hook memoizes the context object from the first render. What I would need during validation is access to the context object passed to useForm function during the most recent render.
from documentation.
@bluebill1049 Yes, that makes sense. Just note that currently validationContext is being cached in useForm from the first render (see https://codesandbox.io/s/react-hook-form-validationresolver-640tr). As of now, the only way to work around this is to create a memoized validationContext object on the app level and pass that to the useForm hook. Passing a fresh validationContext object on each render will not work.
I think it is due to handleChangeRef.current which is a closure over the first validationContext object passed to the hook (see https://github.com/react-hook-form/react-hook-form/blob/master/src/useForm.ts#L515)
from documentation.
in fact can you try it in this codesandbox? https://codesandbox.io/s/react-hook-form-validationresolver-cq2rh
from documentation.
I will update the doc on this part:
validationResolver: will be cached inside react-hook-form, because you want to cache the validation rules instead change during render, same as validation schema.
validationContext: is not cached and you can change then as your component gets re-rendered.
let me know if above makes sense.
what about just creating a setValidationSchema and letting developers call setValidationSchema whenver they need to apply a different schema to the entire form? I'm currently trying to implement the resolver but not having any luck.
from documentation.
I will update the doc on this part:
validationResolver: will be cached inside react-hook-form, because you want to cache the validation rules instead change during render, same as validation schema.
validationContext: is not cached and you can change then as your component gets re-rendered.
let me know if above makes sense.what about just creating a setValidationSchema and letting developers call setValidationSchema whenver they need to apply a different schema to the entire form? I'm currently trying to implement the resolver but not having any luck.
what's your issue? next major version resolver
will be the standard way to validate external schema and conditions.
from documentation.
Related Issues (20)
- Issue: Documentation link incorrect HOT 1
- Typo: "recommend" instead of "recommended"
- issue: Application error: a client-side exception has occurred (see the browser console for more information).
- Improve documentation for useWatch exact
- Update twitter icon HOT 2
- issue: useFieldArray Unable to track newly added elements When "shouldUnregister" is enabled HOT 1
- The `RegisterOptions` type does not match the documentation
- Document watch callback type parameter
- issue: In the specified sandbox environment, when the onChange event handler is invoked with undefined as its argument, the Component does not receive the updated value. However, despite this, the form data is successfully updated. HOT 4
- issue: maxLength does not limit input field to length HOT 1
- Two different documentation versions
- issue: Website appears as a blank background HOT 2
- Documentation: keepIsSubmitSuccessful missing from documentation
- 🥇 Features : Scrollspy or scrolling navigation on documentation site HOT 2
- Feature Request: Implement Scrollspy for Dynamic Navigation
- issue: typo/grammar-issues in /docs/useform/seterror HOT 2
- form builder: "input creator" form state not updated correctly when switching between inputs
- Beekai.com outbound connection on homepage? HOT 1
- Improve documentation on setError HOT 1
- Invalid dependency tree from npm's pov 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 documentation.