for a captcha purpose, I'm trying to set a random number in the hook_form_user_register_form_alter

For example :

function my_module_form_user_register_form_alter(&$form, &$form_state) {
    $random_case = rand(0, 3);
    $form["random"] = array(
        "#type" => "textfield"
        , "#title" => ""
        , "#disabled" => 0
        , "#value" => $random_case
        , "#description" => null
        , "#weight" => -2

When cache is disabled it's working as intended.

when I enable the cache the random number is always the same until I flush all caches. I tried to flush cache programmatically, but it doesn't seem to work. What can I do ?

if I put a


It's working but I think it's because drupal can't put the hook inside the cache.

Best regards.

1 Answer 1


When the forms cached, it caches the original, unprocessed $form array, and the builder functions are not called again.

you can explicitly set cache off by setting $form_state['no_cache'] = TRUE;. Note that this has potential to break Ajax forms.

  • I found a hack but I wasn't totaly pleased. It was putting $GLOBALS['conf']['cache'] = FALSE; inside the hook_init. I prefer and adopt your way. Thank you.
    – Su4p
    Mar 9, 2015 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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