Hi all,

I am using Drupal 4.7. I have a the following problem. I have defined a _menu hook function that displays a "add" tab. When clicked, the add tab should call a function that returns a form. I need to use drupal_get_form to generate the form. However when I do this I just get errors (see below). It seems the framework goes into a endless loop. There seems to be a problem with the $fomr[#parents] variable but I do not defined this myself and I am at a loss on how to fix it. I have googled and only found this (http://drupal.org/node/119621) would did not help.

sample code:
=========

function my_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(	'path' => 'admin/content/my', 
				'title' => t('videos'),
				'access' => user_access('edit own'),
				'callback'=>'my_list');
    $items[] = array(	'path' => 'admin/content/my/list', 
				'title' => t('list'),
				'access' => user_access('edit own'),
				'type' => MENU_DEFAULT_LOCAL_TASK);
    $items[] = array(	'path' => 'admin/content/my/add', 
				'title' => t('add'),
				'access' => user_access('edit own'),
				'callback'=>'my_add',
				'type' => MENU_LOCAL_TASK);
	}
	return $items;
}

function my_add($id=-1) {
 $my=array();
 if ($id!=-1){
	$my = get_my($id);
  }
  $form['my']['file_name'] = array(
					'#type' => 'file', 
					'#title' => t('File'), 
					'#required' => TRUE, 
					'#default_value' => $my->file_name, 
					'#weight' => -4);
  $form['caption'] = array(
					'#type' => 'textarea', 
					'#title' => t('Caption'), 
					'#default_value' => $my->caption, 
					'#rows' => 5, '
					#required' => TRUE);

$form['video']['#attributes']['enctype']= 'multipart/form-data';
$form['video']['submit'] = array('#type' => 'submit', '#value' => t('Submit'));					
return drupal_get_form('my_add',$form);
}

Errors
===================

Warning: Invalid argument supplied for foreach() in /var/www/localhost/htdocs/barloworld/includes/form.inc on line 360

Warning: array_shift() [function.array-shift]: The argument should be an array in /var/www/localhost/htdocs/barloworld/includes/form.inc on line 500

Warning: array_shift() [function.array-shift]: The argument should be an array in /var/www/localhost/htdocs/barloworld/includes/form.inc on line 500

Warning: array_shift() [function.array-shift]: The argument should be an array in /var/www/localhost/htdocs/barloworld/includes/form.inc on line 500

Warning: array_shift() [function.array-shift]: The argument should be an array in /var/www/localhost/htdocs/barloworld/includes/form.inc on line 500

Warning: array_shift() [function.array-shift]: The argument should be an array in /var/www/localhost/htdocs/barloworld/includes/form.inc on line 500

Warning: array_shift() [function.array-shift]: The argument should be an array in /var/www/localhost/htdocs/barloworld/includes/form.inc on line 500

Warning: array_shift() [function.array-shift]: The argument should be an array in /var/www/localhost/htdocs/barloworld/includes/form.inc on line 500

Warning: array_shift() [function.array-shift]: The argument should be an array in /var/www/localhost/htdocs/barloworld/includes/form.inc on line 500

Warning: array_shift() [function.array-shift]: The argument should be an array in /var/www/localhost/htdocs/barloworld/includes/form.inc on line 500

Warning: array_shift() [function.array-shift]: The argument should be an array in /var/www/localhost/htdocs/barloworld/includes/form.inc on line 500

Warning: array_shift() [function.array-shift]: The argument should be an array in /var/www/localhost/htdocs/barloworld/includes/form.inc on line 500

Warning: array_shift() [function.array-shift]: The argument should be an array in /var/www/localhost/htdocs/barloworld/includes/form.inc on line 500

..... and on and on.

[Edit: Added <code> and </code> tags around code: nevets ]

Comments

nevets’s picture

Some are cosmetic, others may cause you problems. First up you declare $my as an array, but use it as an object ($my->file_name). You have "extra" levels in your form ($form['my']['file_name'] instead of $form['file_name']), generally not a problem but it makes the code harder to read since things are not consistent. One possible issue is $form['video']['#attributes']['enctype']= 'multipart/form-data'; since ['#attributes']['enctype'] applies to form I have always used $form['#attributes']['enctype']. In the code you posted the field for 'captions' has the '#required' tag with a newline and white space in it, that may be an issue. Taken all but the array vs object issue I would have coded it as

function my_add($id=-1) {
	$my=array();
	if ($id!=-1){
		$my = get_my($id);
	}
	$form['file_name'] = array(
		'#type' => 'file',
		'#title' => t('File'),
		'#required' => TRUE,
		'#default_value' => $my->file_name,
		'#weight' => -4);
	$form['caption'] = array(
		'#type' => 'textarea',
		'#title' => t('Caption'),
		'#default_value' => $my->caption,
		'#rows' => 5, 
		'#required' => TRUE);
	
	$form['#attributes']['enctype']= 'multipart/form-data';
	$form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
	return drupal_get_form('my_add',$form);
}

If this does not help post the lines in form.inc around 360 and 500 might help (my version does not line up with the errors)

mxc4’s picture

Thanks for the help.

I turned out to be that there are several opening single quotes ' on the wrong line. I better turn my screen res down as it was hard to see this using scite on high res screen. I must be getting old.