2

I can set #default_value but there is no #value for text fields?

http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/7#textfield

Is there a work around?

4

Form API is an abstraction over regular HTML form elements, so there are cases where attributes don't match up exactly.

In the context of Form API, #value is for values you don't want the user changing, and doesn't make sense with respect to text <input> controls.

Instead, you want to use #default_value, which is for values you do want the user to change. That is:

$form['mytextfield'] = array(
   'type' => 'textfield',
   '#default_value' => 'Foo',
);

is the same as:

<input type="text" value="Foo" />

If you want to mimic the behavior of Form API's #value (i.e. provide a default value and prevent the user from changing it), use the #disabled property:

$form['mytextfield'] = array(
   'type' => 'textfield',
   '#default_value' => 'Foo',
   '#disabled' => TRUE,
);

For more information about the internals, check out form_builder(), which constructs the render array for the form and _form_builder_handle_input_element(), which replaces #value with the value of #default_value prior to rendering.

1
  • 1
    Thank you for the feedback, that makes way more sense. When I set the field to #disabled though, the value is always emptied in _presave. Though closed, I'm wondering if drupal.org/node/687572 has crept back in. – Citricguy Aug 17 '11 at 1:22

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.