Hidden form values not handled

davidbuckley - July 7, 2009 - 22:57

I have searched the forums and support pages for help in this topic, but I can not seem to find an answer. There is so little support on my issue, that I am sure I am missing something that is very simple. Forgive me for the length of this post, but I wanted to include as much detail as possible.

I am running 6.13 Drupal and have implemented a custom module. I have created a custom form and added several elements using the Administer » Content management » Content types page. Depending on who is entering the form data (certain users), some fields are hidden from view and have a default value set. When a permission-ed user sees all the form items, and then the user enters data and submits the form, the database is updated as would be expected. However, if the form field type is hidden (non-permission-ed users), then when the user selects the submit button, the data gets posted, however the database does not get updated for the hidden form fields only. Only the non-hidden elements make it to the database.

In my custom module, I am setting the hidden form elements in mymodule_form_alter function:

function mymodule_form_alter(&$form, &$form_state){

$form['title']['#value'] = "General Information";
$form['title']['#attributes'] = array('readonly' => 'readonly');
$form['field_mymodule_date']['#value'] = 9999;
$form['field_mymodule_date']['#default_value'] = 9999;
$form['field_mymodule_date']['#attributes'] = array('readonly' => 'readonly');
$form['field_mymodule_date']['#type'] = 'hidden';
$form['field_mymodule_date']['#required'] = TRUE;

}

All seems well at this point. I can view the data that has been set in the function mymodule_form_submit

function mymodule_form_submit($form, &$form_state){

drupal_set_message(''. print_r($form, TRUE) .'','warning');
drupal_set_message(''. print_r($form_state, TRUE) .'','ok');

}

The $form array has the hidden form element in it (field_mymodule_date):
Array
(
[#id] => node-form
[nid] => Array
(
[#type] => value
[#value] =>
[#post] => Array
(
[changed] =>
[form_build_id] => form-00b550491c493892c4025d37fc70edcf
[form_token] => e1651af4d1fcaaae285bc90215d908a5
[form_id] => mymodule_node_form
[field_mymodule_date] => 9999
[title] => General Information
[teaser_include] => 1
[body] => AAAAAA
[format] => 1

)
)

[vid] => Array
(
[#type] => value
[#value] =>
[#post] => Array
(
[changed] =>
[form_build_id] => form-00b550491c493892c4025d37fc70edcf
[form_token] => e1651af4d1fcaaae285bc90215d908a5
[form_id] => mymodule_node_form
[field_mymodule_date] => 9999
[title] => General Information
[teaser_include] => 1
[body] => AAAAAA
[format] => 1

)
)

[uid] => Array
(
[#type] => value
[#value] => 1
[#post] => Array
(
[changed] =>
[form_build_id] => form-00b550491c493892c4025d37fc70edcf
[form_token] => e1651af4d1fcaaae285bc90215d908a5
[form_id] => mymodule_node_form
[field_mymodule_date] => 9999
[title] => General Information
[teaser_include] => 1
[body] => AAAAAA
[format] => 1
)
)

------------------
The $form_state also has the hidden form element in it (field_mymodule_date):

Array
(
[storage] =>
[submitted] => 1
[values] => Array
(
[nid] =>
[vid] =>
[uid] => 1
[created] => 1247004705
[type] => mymodule
[language] =>
[changed] =>
[title] => General Information
[teaser_js] =>
[teaser_include] => 1
[body] => AAAAAA
[format] => 1
[revision] => 0
[log] =>
[name] => admin
[date] =>
[status] => 1
[promote] => 1
[sticky] => 0
[op] => Save
[submit] => Save
[preview] => Preview
[form_build_id] => form-00b550491c493892c4025d37fc70edcf
[form_token] => e1651af4d1fcaaae285bc90215d908a5
[form_id] => mymodule_node_form
[field_mymodule_date] => 9999

[#post] => Array
(
[changed] =>
[form_build_id] => form-00b550491c493892c4025d37fc70edcf
[form_token] => e1651af4d1fcaaae285bc90215d908a5
[form_id] => mymodule_node_form
[field_mymodule_date] => 9999
[title] => General Information
[teaser_include] => 1
[body] => AAAAAA
[format] => 1
)
)

[redirect] =>
)

--------------------------

As well, when I print out $_POST from the mymodule_form_submit function, I see the hidden form data:
Array
(
[changed] =>
[form_build_id] => form-2d92716a7f6f88cbaba7682cfb8e5c00
[form_token] => e1651af4d1fcaaae285bc90215d908a5
[form_id] => mymodule_node_form
[field_mymodule_date] => 9999
[title] => General Information
[teaser_include] => 1
[body] => AAAAAA
[format] => 1
[menu] => Array
(
[link_title] =>
[parent] => primary-links:0
[weight] => 0
)

[book] => Array
(
[bid] => 0
[plid] => -1
[weight] => 0
)

[log] =>
[pathauto_perform_alias] => 1
[comment] => 2
[name] => admin
[date] =>
[status] => 1
[promote] => 1
[op] => Save
)

While I see the values in the $form, $form_state, and in $_POST, when the form is submitted, the hidden elements do not make it to the database. If the elements are not hidden (i.e set to textfield for a permission-ed user) and the user enters data, then the data makes it to the database.

I have tried setting 'type' = 'hidden' as well as 'type' = 'value'. I have used both #default_value as well as #value for the 'value' element based on some entries in the forums.

Thanks in advanced for helping

In debugging this further, I

davidbuckley - July 8, 2009 - 03:10

In debugging this further, I see the following behavior when I check the field_entry_date value inside the modules/cck/content.module

in function content_field -
function content_field($op, &$node, $field, &$items, $teaser, $page) {
drupal_set_message('op = '.$op,'ok');
drupal_set_message(''. print_r($node->field_journal_date, TRUE) .'','ok');

I get the following:

* op = validate
* 9999

* op = validate
* 9999

* op = validate
* 9999

* op = presave
* 9999

* op = presave
* Array
(
[0] => Array
(
[value] =>
)

)

* op = presave
* Array
(
[0] => Array
(
[value] =>
)

)

It looks like it loses its value.

David Buckley

The North Face JacketsBest choice

xiaokuan33 - July 20, 2009 - 02:57

Having been the most trustful brand for outdoor clothing and equipment since 40 years ago, The North Face is producing some of the finest apparel for nearly every outdoor sports such as trail running and backcountry skiing.
North Face Jackets
North Face Fleece
Northface Jacket
North Face Jackets
Their tents and equipment are used by top mountainers throughout the world due to their excellent and important performance and reliability. Not only does The North Face have the style for everyday use around the town, but also the performance to travel across even the most far-away mountains. Supposing you are new to it, a good place to start is the North Face jackets, for men, women and kids.

Hidden form values not handled

davidbuckley - July 19, 2009 - 19:42

The trick was to use hook_nodeapi()

David Buckley

 
 

Drupal is a registered trademark of Dries Buytaert.