Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a form with some disabled form elements. When the form fails validation and is sent back to the user, the disabled fields are empty. How can I make the disabled fields display the default values that were originally defined in the form?

function foo_form(&$form_state) {
  return array(
    'foo' => array(
      '#type' => 'textfield',
      '#default_value' => 'foo',
      '#disabled' => true,
    ),
    'submit' => array(
      '#type' => 'submit',
      '#value' => 'Submit',
    )
  );
}

function foo_form_validate($form, &$form_state) {
  form_set_error('submit', 'You cannot submit this form');
}

In this example, when the user presses the Submit button, the expected warning is displayed, but the text field is empty. This is most disturbing for the user, he does not know that all relevant information to process the form are still present.

$form_state['values']['foo'] is empty, but that's expected because disabled HTML elements are not included in the HTTP request that submits the form. Setting $form_state['values']['foo'] to the desired value in the validation function does not help.

share|improve this question

1 Answer

up vote 2 down vote accepted

The function that builds the form array isn't expecting any params unless you set a param in your hook_menu for instance. Instead you could try to grab the $form variable in the validation handler by reference and setting the value.

function foo_form() {
  return array(
    'foo' => array(
      '#type' => 'textfield',
      '#default_value' => 'foo',
      '#disabled' => true,
    ),
    'submit' => array(
      '#type' => 'submit',
      '#value' => 'Submit',
    )
  );
}

function foo_form_validate(&$form, &$form_state) {
  $form['foo']['#value'] = 'bar';
  form_set_error('submit', 'You cannot submit this form');
}

This is untested as I have no running Drupal 6 instance at the moment. But I was able to do so using Drupal 7.

Another approach would be to use #value instead of #default_value since you already got your custom validation handler to put some extra logic in (get rid of the form_state value).

share|improve this answer
Awesome answer! – Gaelan Jun 28 at 5:08
$form is never passed as reference, not even in Drupal 6. – kiamlaluno Jun 28 at 5:15
You're right, Drupal isn't. Even $form_state isn't. This is why we are putting a leading & before. Don't get me wrong, I never used the $form as a reference in a validation function before but I was curious and tested it and it worked in D7. So please be more verbose. Your comment isn't getting anywhere. Is it style, performance or what? Since technically it's possible. – aaki Jun 28 at 5:27
1  
If you will set #value, user input will get ignored, only what you set in #value will be used from now on. Tested. – Mołot Jun 28 at 8:27
This is right but since he wants this field to be disabled... – aaki Jun 28 at 8:33
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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