Hi,

I'm trying to build a form with the API, and I've run into a problem with disabled fields. Basically, I want a bunch of input fields to be disabled by default (they're also hidden with CSS), and then switched on by javascript later. The problem is that whenever the form gets submitted, the values for disabled forms are also being sent -- I don't want it to do this. Maybe this is something really obvious and I'm just drawing a blank. These are hidden input fields, if that matters. I did try a non-drupal test using just the HTML output and setting the form method to GET. In that case, it did not send disabled fields, so I'm guessing my problem either has to do with how Drupal is processing the form data, or maybe there's a difference with POST.

Anyway, here's a code sample:

	$form['r3_c1'] = array('#type' => 'hidden',
				'#title' => t('Row 3. Column 1'),
				'#required' => FALSE,
				'#default_value' => 0,
				'#disabled' => TRUE,
			);

Any suggestions?

Thanks in advance.

Comments

rszrama’s picture

The disabled attribute really just means the form element is "disabled" on the page. This means its value can't be changed, but it will still submit a value when the form is submitted. If they weren't hidden, like a textfield, they would appear "grayed out" with their value shown but unable to be modified.

I actually don't know what purpose a disabled hidden input field would serve, since you can't modify those on forms anyways. What exactly are you trying to accomplish?

----------------------
Current Drupal project: http://www.ubercart.org

zwhalen’s picture

You can always un-disable an input field with javascript, make changes, then submit, which is basically what I'm setting up here. In the past I've never needed to not send some fields (i.e., I'd just ignore any values from fields not necessary for the given situation), but I do in this case. In my standalone testing (just an HTML form with GET method), it definitely is the case that disabled form elements are not sending data. That may not true with POST, but I haven't tested yet; it's probably the same. Quirksmode, at least, states it uncategorically that "disabled form fields are not sent back to the server", but he doesn't mention hidden fields.

Basically, there are a couple of different variables here, but I'm trying to figure out if there's any reason why Drupal would process a disabled form field if it's not supposed to.

The reason I'm disabling and enabling hidden fields is that I'm sort of constructing a workaround that mimics the logic of check boxes has the appearance of a grid of pixels. So clicking a table-cell darkens it and also sets the value of the field that's hidden there. Styling checkboxes is unreliable, and the visual appearance of this form is crucial to its usefulness, so I think this is a pretty good solution for my needs -- if I can get this disable thing featured out. (The disabling / enabling comes in when I add or remove rows, incidentally)

rszrama’s picture

Perhaps the problem is you're using #default_value instead of just #value? See if that solves it. Otherwise, you can always use $_POST to check the input and unset the keys in $form_values the weren't modified or passed in on the form submission.

----------------------
Current Drupal project: http://www.ubercart.org

torvall’s picture

In my case, changing from setting #default_value to setting #value instead on a disabled (text) field made it return it's value on $form_state['values'][item].

dipen chaudhary’s picture

please look at
http://api.drupal.org/api/file/developer/topics/forms_api_reference.html...
http://api.drupal.org/api/file/developer/topics/forms_api_reference.html...

notice the text in bold,

--------------------------------------------------------
Dipen Chaudhary
www.dipenchaudhary.com

--------------------------------------------------------
Dipen Chaudhary
www.qed42.com ( Drupal development services specialize in social networks and other user generated content platforms )
www.dipenchaudhary.com

budda’s picture

I'm using hook_form_alter() to disable some taxonomy select menus. However upon form submission their values are *not* being sent, and thus when the node is saved, it looses the previously set taxonomy terms.

Annoying, but i can't see a solution at the moment.

--
Ixis (UK): Drupal consultancy, Drupal hosting.

alexgreyhead’s picture

This might be too late for buddha, but instead of unsetting or hiding the field, try setting ['yourfield']['#access'] to 0 and try sending the form again. In D6 at least it tends to preserve values, although you will probably run into problems if the hidden element contains a required value.

executex’s picture

Actually,

#disabled => TRUE

causes text fields to not be submitted via POST data. Might be a bug in Drupal. $form_state['values']['aDisabledField'] will == empty.

#disabled => TRUE for other fieldtypes, seems to work fine.

I'm trying to do the opposite, force it to submit all. Since I use AHAH, I don't want #required => TRUE. I check empty in validate() hook.

dman’s picture

Against my expectations, and 16 years of thinking I knew about HTTP, browsers apparently are not expected to send disabled form submissions back in the POST.
see other thread

If you use #disabled => TRUE, you should also use

$element['#value'] => $element['#default value'];

to enforce it, and ensure FAPI knows what the value is supposed to be.

If, OTOH, you are using AJAX .. you need to do something else.

dylanb’s picture

If you want to disable a field then you should amend that element in the $form array like so...

$form['field_to_disable']['widget'][0]['value']['#attributes']['disabled'] = 'disabled';

However, this results in the value of this field not getting submitted so if you have a prepopulated field on display that the user should not alter then forget about the above snippet and do the following instead.

First, I used this CSS to prevent a user from clicking on the field...

.form-wrapper .form-item input#edit-field_to_disable-0-value {
  pointer-events: none;
}

...and this in hook_form_alter() to prevent a user from tabbing into the field...

$form['field_to_disable']['widget'][0]['value']['#attributes']['tabindex'] = '-1';

If you want the field to look like it has the `disabled` attribute then you will need other CSS to make that happen.

edrigor29’s picture

Hello Drupal Community, I need help. I want to know how disable the default "Title" sitting in the content type field. I'm planning to create 2 views one for the "Generel discussion" and one when it gets fired up and lead to another node such as this.

When this gets fired up:

http://forum.koramgame.com/forum.php?gid=96

It leads to this:

http://forum.koramgame.com/forum-99-1.html

But i'm afraid i wont be able to make it because of that default "Title" field sittng pretty inside the content type. Do you think this requires a custom configuration in PHP to disable the title field, i'll replace it with an entity reference field type.

Let me know your thoughts about this one. Thanks.
Show less