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.