-
|
Hi There. I'm having some troubles with let dynamicValue = condition ? 1 : 2
const {
register,
trigger,
control,
handleSubmit,
watch,
formState: { errors },
getValues,
setValue,
reset,
} = useForm({
reValidateMode: "onChange",
resolver: yupResolver(carDetailsSchema, {
context: {
maxMarketValue: dynamicValue,
},
}),
shouldUnregister: true,
}); marketValue: yup
.number()
.min(0)
.when("$maxMarketValue", (maxValue, schema) => {
console.log(maxValue);
return schema.min(0).max(maxValue).required();
}),
```
However when I try to log the maxValue to the console is it undefind? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
|
not sure why it's not having the context, i can see the source code: https://github.com/react-hook-form/resolvers/blob/master/yup/src/yup.ts#L41-L82 |
Beta Was this translation helpful? Give feedback.
-
|
As I remember, The current implementation of yup resolver is using |
Beta Was this translation helpful? Give feedback.
-
|
Hi, does anyone know how to do the same in useForm({
context: {something: "something"},
resolver: zodResolver(mySchema)
)I can't find a way to do it. It seems to work only in |
Beta Was this translation helpful? Give feedback.
-
|
@bluebill1049 Since this behavior can be unintuitive and is currently easy to miss, I was wondering if it would be acceptable to open an issue (and follow up with a PR) to update the react-hook-form/resolvers README. My intention would be to clearly document how context is handled so users can avoid unnecessary debugging and understand the correct usage upfront. Please let me know if this sounds reasonable. I’d be happy to contribute the documentation update. Thanks in advance. |
Beta Was this translation helpful? Give feedback.
@Notaduck @bluebill1049 :
As I remember,
contextwill be provided viauseForm({ context: { // Some context props } }), not from resolver schema options 😄The current implementation of yup resolver is using
Object.assign({}, schemaOptions, { context // this is the context from react-hook-form })therefore the react-hook-form context always overrides the "context" from schemaOptions.