Errors when submitting contenttype_node_form via druapl_execute() when contenttype contains date field
| Project: | Date |
| Version: | 6.x-2.3 |
| Component: | Date API |
| Category: | bug report |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
I've been running into issues with date fields when I try to remote control the _node_form with drupal_execute() in the custom module I'm building.
My form builder builds a skeleton node form, then creates an array of CCK fields and adds them to the form via a utility function that I found code for elsewhere on the site (code below). All of the CCK fields have been added to the content type, the form builds correctly and displays correctly.
If I leave the date blank (it's not required) and submit, I get no errors and the node is created.
If I fill in a date, and submit, I've had the following two errors:
I had the date field high in the array, and got:
There are errors in Date:
* The dates are invalid.I moved it to the end of the array and now get:
An illegal choice has been detected. Please contact the site administrator.I don't have a lot of time to debug further right now, I am on a deadline and have to seek alternate options. I look forward to seeing if this can be resolved. I should have more time to look into it in a week or two.
Here's a pared down version of the code I'm using, it's worked for about 10 or 15 other custom forms I built for editing subsets parts of a really complex content type:
<?php
/*
This function gets is registered in hook_menu() as a page callback
*/
function modulename_page($form_state, $nid) {
return drupal_get_form('modulename_form', $nid);
}
/*
Form builder
*/
function modulename_form($form_state, $nid) {
$profile_node = node_load(array('nid' => $nid));
module_load_include('inc', 'node', 'node.pages');
$form = array();
//needed for node form to work
$form['#node'] = $profile_node;
//list of CCK fields to add to form
$fields = array(
'field_profile_date', //a CCK Date field - this generates errors
'field_profile_firstname', //a CCK Text field - this works
'field_profile_lastname', //a CCK Text field - this works
'field_profile_image', //a CCK File field - this works
);
$form = _modulename_load_cck_fields($fields, $form, $form_state);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Information'),
'#weight' => 100,
);
return $form;
}
/*
form submit hook
*/
function modulename_form_submit($form, &$form_state) {
module_load_include('inc', 'node', 'node.pages');
$node = $form['#node'];
$profile_state = $form_state;
$profile_state['redirect'] = '';
$profile_state['values']['op'] = t('Save');
drupal_execute('contenttype_node_form', $profile_state, $node);
drupal_set_message('Your form has been saved');
}
/*
Utiltiy function for adding cck fields to a form
*/
function _modulename_load_cck_fields($fields, $form, $form_state) {
module_load_include('inc', 'content', 'includes/content.node_form');
foreach ($fields as $name) {
$field = content_fields($name);
$form['#field_info'][$name] = $field;
$origForm = (array) content_field_form($form, $form_state, $field);
$form += $origForm;
}
return $form;
}
?>Thanks in advance for any help.

#1
I have the same problem.
I just needed to run a one-time import which used drupal_execute to run node_forms a couple of hundred times(once per node) and a workaround that seamed to work was to disable the date module when I run the import and then re-enable it once the import was done.This keeps the date fields data that was there before the disabling of the module.Not true, do not use this method! Data may be lost!
I fixed it by using suggestion mentioned here: http://drupal.org/node/477392#comment-2111134
#2
See http://drupal.org/node/477392#comment-2111134, it may help in this situation. The Services module uses drupal_execute as well.
#3
I am using for date field date_popup widget. When I try to save form partially (i.e. only one group of fields) I am getting same error on fields which contains default value (i.e. been saved before). After debugging i've found that the date_popup widget cannot parse the value in
date_popup_input_valuefunction.The for is loaded with previosly saved value which is passed to function $element['#value'] = '2009-11-02 10:00'. The
date_popup_input_valueis expecting the $element['#value']['date'] - the widget format of value. Therefore the default value isn't parsed and the validation fails.#4
Probably related to #408770: #access of date_combo fields prevents validation fix.