On my site I do this:

I got a form with this field:

'myfile' => [
  '#title' => $this->t('MyTitle'),
  '#type' => 'managed_file',
  '#upload_location' => 'public://myfolder',

And MyController:

$myform = \Drupal::formBuilder()->getForm('Drupal\[mymodule]\Form\[myclassform]');

This variable I pass to Twig template.

On my template:

{{ myform }}

Every is ok.

But when the page is cached I got this error when i upload a file:

An unrecoverable error occurred. Use of this form has expired. Try reloading the page and submitting again.

I dont use memcache or CacheRoute, only drupal cache for anonymous user.

How I fix that? Is possible not cached the form?

  • Where are you embedding that form? I guess in something that is being cached, like an entity or a block? – Berdir Mar 9 '15 at 21:51
  • @Berdir I embed the form on a custom page, I do it like this – rpayanm Mar 9 '15 at 22:41

I fixed that prevent that drupal cached the page, this way, because the forms can't be cached:

// Deny any page caching on the current request.    


Recently was commited Add #cache[max-age] to disable caching and bubble the max-age

When $build['#cache']['max-age'] is not set, permanent cacheability (Cache::PERMANENT) is assumed.

To indicate that a render array is not cacheable at all, set $build['#cache']['max-age'] = 0 (i.e. zero seconds).

And to indicate that a render array is cacheable only for a limited amount of time, e.g. 5 minutes, set $build['#cache']['max-age'] = 300; // 5 * 60 = 300.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.