-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Calling setFieldValue in response to a field change is out of sync with validations #2059
Comments
Looks like I experienced the same issue today after migrating from |
The gist is not loading for me on my network but I was having a similar issue and figured I'd mention it in case it helps someone else while upgrading.... I had multiple selects using setFieldValue but the validation always seemed to be one step behind. In my change handler I was calling setFieldValue and then setFieldTouched to touch and set the value of the field. Both of these functions trigger the validate function and it looks like the setFieldTouched function was returning last and running using the value of the field before it was updated. This showed the field as invalid even though it had a value. In short I just updated the setFieldTouched function to not validate the form.
|
@njetsy i did consider that, but i also wanted the form validating still, showing the user they need to fill out the second drop down if they already submitted. In my case i wrapped my setFieldValue() call in a requestAnimationFrame callback, which is very much a hack. |
I just found another form that's probably closer to yours where I have to clear menu 2 after selecting a new menu 1 option. When I call setFieldValue to assign the newly selected value to menu 1, it runs the yup validation successfully with the correct values. When I then call the second setFieldValue to clear menu 2, a console log within the yup validation shows the menu 1 value as still being null so it shows the field as invalid. |
I'm running into a similar issue. I have a select dropdown (only 1.) and when I call form.setFieldValue(), the validation runs, but it fails for some reason until I click somewhere on the screen. I'm also running into this after upgrading to Formik 2.x |
In my case, I enabled |
I had a similar issue and solved it by calling const handleChange = (value: string) => {
formik.setFieldValue(name, value);
setTimeout(() => formik.setFieldTouched(name, true));
}; |
worked me Is there any other solution because it is ultimately a problem or not? |
I am also facing the same issue with "formik": "^2.1.4". |
I've also run into this problem, also with formik ^2.1.4. The workaround below is working for me as well.
|
I find that I have to call |
Pay attention that function setFieldValue in formik is an async function(it returns a Promise), therefore, when you want to change x field value as a result of y field , you have to put the calling for setFIeldValue on y field in a callback to the first setFieldValue call: for example: onChange= (_, value) => { (or using async await syntax sugaring) |
Solution without const handleChange = async (value: string) => {
await formik.setFieldValue(name, value);
formik.setFieldTouched(name, true)
}; |
^ it seems the typescript definition for setFieldValue is wrong; it shows return value as void but this fix worked great for me when nothing else did. |
@sjungwirth Is there a simple workaround to give typescript the correct definition? I'm running into the same issue with this. |
For some reason the solution without setTimeout doesn't work for me. But it works fine with delayed setFieldTouched |
@sapkra @jjarcik @bswank @drudv and others using either the setTimeout method or async awaiting setFieldValue: Have you tried using |
I was just bitten by this issue and I'm confused on what grounds has this issue been closed. There is no resolution or recommendation attached to closing it. I would expect to at least fix the bug in the docs or ideally provide a recommendation or even a link to the docs where this is explained. (On a higher level, an API where one has to |
this guy has awesome solution https://stackoverflow.com/a/57684307/4445575 |
I'm still getting this issue at version onChange={(newValue) => {
setFieldValue('my-field-name', format(newValue), true); // 3rd param means should validate, but it's not working, I guess
setTimeout(() => setFieldTouched('my-field-name', true)); // this fixes the issue
} |
setTimeout solved the issue. thanks! |
If you need validate a few values after change one value, you can use Promise.all const handleChange = (
formik,
value,
[name0, name1, name2, name3, name4, name5] // will best use param and in promise use param.map
) => {
const setAllFields = Promise.all([
formik.setFieldValue(name0, value),
formik.setFieldValue(name1, getName1(value)),
formik.setFieldValue(name2, getName2(value)),
formik.setFieldValue(name3, getName3(value)),
formik.setFieldValue(name4, getName4(value)),
formik.setFieldValue(name5, getName5(value)),
]);
setAllFields.then((res) => {
Object.keys(res[0]).forEach((key) => {
formik.setFieldTouched(key, true);
});
});
}; |
This solved the issue for me. Thank you! |
I had the same "trouble" and saw this issue here at GitHub today, there is still no update in the docs and types.d.ts-file of the packages, at least what I can see?! still: I had to overwrite the types in my project manually and then it works fine with async-await - but
Are there any plans to update this - or do I miss something? :) |
I run into this. My solution, call |
does Formik receive any updates on this topic and in general in a whole codebase? const setFieldValueAndValidate = async (field: string, value: unknown) => {
await setFieldValue(field, value);
validateField(field);
}; |
Just fyi, I'm still getting this on |
|
Shouldn't the setFieldTouched() be called inside the setFieldValue() as part of default implementation of setFieldValue ? |
2.2.9 has been released for a long time as the latest version, and this issue still exists. My project was "working" accidentally because of a useEffect block that was effectively triggering on every re-render which called validate() similar to what @waymaha suggested. "Fixing" that useEffect causes this issue to occur. Should I be using a different version of Formik at this point? Is Formik still maintained? I see that the PR mentioned a couple of years ago that fixes the type definition is open: #2384 Using set timeout is hacky. A useEffect block is a little less hacky, but still not great since you have to remember to add this useEffect to get what should be default behavior. |
@jaredpalmer would you mind re-opening this issue please? |
Anyways, for others coming across this, I was able to get it to work with the following: const handleMyFieldChange: ChangeEventHandler<
HTMLInputElement
> = async (e) => {
if (e.target.value === 'no') {
await setFieldValue('my_other_field', DEFAULT_VALUE)
setFieldTouched('my_other_field', DEFAULT_TOUCHED)
}
} Where The |
|
## Description Describe the changes or additions included in this PR. Fix the error we use `setFieldValue` in Formik leading to validation error see [issue](jaredpalmer/formik#2059) ## Test Plan Before https://user-images.githubusercontent.com/126525197/233100066-e41f0173-e544-4f66-b6eb-da156d84b2d2.mov After https://user-images.githubusercontent.com/126525197/233100092-1c0a772a-7442-48d6-b2d8-7db384a854a3.mov How did you test the new or updated feature? --- If your changes are not user-facing and not a breaking change, you can skip the following section. Otherwise, please indicate what changed, and then add to the Release Notes section as highlighted during the release process. ### Type of Change (Check all that apply) - [x] user-visible impact - [ ] breaking change for a client SDKs - [ ] breaking change for FNs (FN binary must upgrade) - [ ] breaking change for validators or node operators (must upgrade binaries) - [ ] breaking change for on-chain data layout - [ ] necessitate either a data wipe or data migration ### Release notes
I am having this same issue on Formik latest version is @brandonlenz 's workaround did work for me, but the impression I get is that touched doesn't really have anything to do with the problem, the fix works simply because it retriggers the validation which should be handled by setValue in the first place. It is the same workaround as calling setValue again on a setTimeout. |
I am having the same issue using
|
馃悰 Bug report
Current Behavior
The example im working with is let's say i have two drop down menus 1 & 2.
When menu1 changes, i want to reset the value of menu2. When I do this, and both fields are required, both menu1 & menu2 are invalid, even though menu1 has a value.
Expected behavior
I am updating the value of menu2 in componentDidUpdate when formik.values.menu1 changes. I feel like calling
setFieldValue('menu2,' '')
should be safe.Reproducible example
https://gist.github.com/agmcleod/921c5798c4ba7285bc12bff8ebd6d82b
I tried to use the code sandbox templates, but yup validations werent working there for some reason.
Suggested solution(s)
Your environment
The text was updated successfully, but these errors were encountered: