When you set

$form['#cache'] = FALSE;

the form API enables the cache. Attached patch fixes that, to enable the cache when it's really set to TRUE.

Note that the problem applies to d6 too, so I've also attached a patch for d6.

CommentFileSizeAuthor
#5 form_cache_6_0.patch601 byteschx
form_cache_d7.patch604 bytesfago
form_cache_d6.patch619 bytesfago
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Damien Tournoud’s picture

Title: $form['#cache'] isn't respected properly » Setting #cache to FALSE shouldn't cache the form
Status: Needs review » Reviewed & tested by the community

This change is long overdue. I discussed it with chx a while ago, and we have his agreement on that.

The behavior implemented by the patch is: "the form is cached if any of its element set #cache to TRUE". It means that setting #cache to FALSE don't have any effect whatsoever. That's only a DX patch.

dropcube’s picture

Title: Setting #cache to FALSE shouldn't cache the form » Cache form only if any of its element set #cache to TRUE

This is issue is breaking the normal behavior of the Form API. The patch is simple and fixes the issue, please commit it ;)

webchick’s picture

Version: 7.x-dev » 6.x-dev

D'oh. :P That looks like a brain-o. :P

Ran all tests, no failures. Committed to HEAD. Setting back for consideration in 6.x.

chx’s picture

Yay for the patch. Nay for the implementation. !empty is your friend.

chx’s picture

FileSize
601 bytes

Patchy.

webchick’s picture

Thanks, reverted the patch in #0 and committed #5 to HEAD. The one in #5 should apply to 6.x without a problem.

Gábor Hojtsy’s picture

Status: Reviewed & tested by the community » Fixed

Great, committed #5.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

bzzz’s picture

Status: Closed (fixed) » Active

Here is the situation:
$form['#cache'] = FALSE; ('#type' = 'form')

but one of the elements have
$form['#cache'] = TRUE

therefore FormAPI changes #cache of whole form to TRUE! Why??

I don't want to cache this form! I told FormAPI not to cache this form!
But someone else add another element to form. And by the way decided to enable cache for this element.

I think we should switch #cache to true only if this parameter isn't set at all.

KingMoore’s picture

I can't get my forms to not cache no matter what I do (if I have drupal caching enabled). #cache doesn't seem to be documented? Am I missing something?

MantasK’s picture

I was searching for something else and found this. It is old but maybe somebody is going to find it useful.
In d6 I also had some issues with cached forms and validation.
I solved problem by implementing:

function form_name_after_build(&$form, &$form_state) { 
  if ($form_state['submitted'] == 1) {
    //escape validation
  
  }
  else {
    return $form;
  }
}

Status: Active » Closed (outdated)

Automatically closed because Drupal 6 is no longer supported. If the issue verifiably applies to later versions, please reopen with details and update the version.