My form that uses ahah_helper will generally be submitted by anonymous users. My issue is that when caching is turned the ahah comes back with form data from a prior anonymous user's submission. The form loads properly so it is not that the storage isn't getting unset by the ahah_helper_real_submit function. Turning off the cache temporarily fixed the issue, but long term, the site needs caching for performance reasons.
I believe that the root cause is when the function ahah_helper_render is called, the form is retrieved from the cache using the form_build_id. At least for me, this form_build_id was the same for all anonymous users so fetching the form from cache grabs someone else's data. Maybe my drupal or implementation is causing it. I tried, unsuccessfully, to find a way to get a new form_build_id generated for anonymous users.
The work around I came to was to call cache_clear_all() at the end of my submit handler. I expect that this is not the best fix, but my form is only going to get used a few times a week so clearing the cache each time is not expensive for me.
I am not sure if clearing the cache is something that the ahah_helper_real_submit should do or if better done in the submit handler of the form.
The cache_clear_all function does allow you to target a specific things in the cache to clear so maybe that is a good compromise. Here is what seems to be a viable option I found in the drupal_process_form function. I will attempt to test at some point soon. I expect that this would make the cache clear expense negligible for most any implementation.
<?php
function ahah_helper_real_submit($form, &$form_state) {
unset($form_state['storage']);
cache_clear_all('form_' . $form_state['values']['form_build_id'], 'cache_form');
}
?>Finally, in my troubleshooting, I noticed in the api that the form_get_cache function passes &$form_state as a reference, but ahah_helper argument has just $form_state. Not sure if this matters here..
Comments
Comment #1
tbrix commentedI am experiencing the same issue.
Did you ever find a solution, that did not imply emptying the cache ? Even with that solution, the problem persist if two anonymous users are filling the form simultaneously. It seems to me that it requires a unique form_build_id to be generated - also for anonymous users ?
Comment #2
tbrix commentedThere is another solution to this. Sites using Drupals Cache, will cache the page including the form. This is the reason the form_build_id is the same for anonymous users. I used the contrib module cache excelude to exclude the paths where the forms where used from caching https://drupal.org/project/cacheexclude. This solved the issue and anonymous users gets unique form_build_id's and the ahah callback works again without merging different users form-posts.
Comment #3
Victor Safronov commentedBy the way, it looks like it's not a bug anymore, because three years after this issue was submitted, the changes happen :) https://www.drupal.org/SA-CORE-2014-002