We can store a permanent value in $form_state in the submission handler, which will be retained against form errors, but I want to know how we can do it while in the validation handler, where I show an error using form_set_error().

In short, I want to show an error in validation with the message "Press submit again to confirm". In this way, if the same form is submitted again it would skip the validation handler.

I can't do it in the submission handler, since it clears the values in the form.

form_set_value() is not able to retain a value.

EDIT (on 28th Dec)

I've found that $form_state['storage'] is works in validator also only if form_state['rebuild] is set to true. Infact this is how we use in submit handler. And if I call form_set_error in the validator form_state['rebuild] is simply ignored even if it is set and the storage value is not retained.

  • Did you try setting a value in $form_state from the validation handler?
    – apaderno
    Commented Dec 27, 2011 at 16:48
  • You mean how we change and save it in submit handler? Yes it does not work.
    – AgA
    Commented Dec 27, 2011 at 18:25
  • No, I mean if you tried setting a value in $form_state from the validation handler, which is then checked in the validation handler when it is again called.
    – apaderno
    Commented Dec 27, 2011 at 19:32
  • If I set a value in $form_state in validation handler then it is not retained when validation handler is called again after form_set_error and resubmit by user.
    – AgA
    Commented Dec 28, 2011 at 4:13

2 Answers 2


$form_state['storage'] is probably what you're looking for. I've used it on multi-step forms myself to preserve necessary data that I need during various stages of the form processing. As an example you can define a storage variable like this:

$form_state['storage']['mykey'] = 'myval';

Edit: Considering the additional information you provided it is indeed not quite as simple in your specific case. Setting $form_state['rebuild'] = TRUE; in the form validation handler also means that the user set form values will be lost. So what I think you could do is the following:

In the form validation handler function:

if (!isset($form_state['storage']['mykey'])) {
  $form_state['storage']['mykey'] = 'myval';
  // Save all the user set form values.
  foreach ($form_state['values'] as $key => $val) {
    // If you want to skip some values do it here.
    $form_state['storage']['values'][$key] = $val;
  $form_state['rebuild'] = TRUE;
  drupal_set_message('Press submit again to confirm');

In your form builder function use the stored values as defaults. Example:

$form['key_1'] = array(
    '#type' => 'radios',
    '#title' => t('Title'),
    '#options' => array(0,1,2),
    '#default_value' => $form_state['storage']['values']['key_1'],

Not sure how well the example code fits your needs, but it should get you started or at least give further ideas/options to try out.

  • That's what i've mentioned that I want to do it in validator and not in the submit handler.
    – AgA
    Commented Dec 27, 2011 at 18:23
  • This can be done in the validation handler and has worked out nicely for me so far.
    – Madis
    Commented Dec 27, 2011 at 18:37
  • In Drupal 7 is not anymore necessary to use $form_state['storage'], as reported in the documentation for drupal_build_form(): "$form_state['storage'] is not a special key, and no specific support is provided for it in the Form API. By tradition it was the location where application-specific data was stored for communication between the submit, validation, and form builder functions, especially in a multi-step-style form." $form_state['storage'] is used in Drupal 6.
    – apaderno
    Commented Dec 27, 2011 at 19:47
  • @Dooshta where are you setting the value? In validation handler or submit handler?
    – AgA
    Commented Dec 28, 2011 at 4:32
  • Edited my answer, check if it helps you solve your problem now.
    – Madis
    Commented Dec 28, 2011 at 14:24

The Unique field module does something similar with respect to overriding form validation. Grab its code and take a look to see if you can modify it to your needs.

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.