Hi,

I've read the forms API multiple times and searched exhaustively but can't find the answer to my problem. I am developing a module and have two forms on the same page. The first form has 3 required select boxes, a textarea field, and a submit button. The second form has a textarea field and a submit button. The problem comes from when I click the submit button on the second form, the required fields for the first form prevent the second form from being submitted. I'm sure I just don't have my form setup correctly. Can you look at this code and tell me what I did wrong?


function hgui_manual() {	
	$form['hgui_manual_import']['hgui_select_type'] = array(
		'#type' => 'select',
		'#title' => t('Type'),
		'#options' => array(
			'' => t(''),
			'test1' => t('test1'),
		),
		'#required' => TRUE,
		'#description' => t('test1.'),
	);

	$form['hgui_manual_import']['hgui_select_break'] = array(
		'#type' => 'select',
		'#title' => t(Break'),
		'#options' => array(
			'' => t(''),
			'test2' => t('test2'),
		),
		'#required' => TRUE,
		'#description' => t('test2.'),
	);
	
	$form['hgui_manual_import']['hgui_select_website'] = array(
		'#type' => 'select',
		'#title' => t('Website'),
		'#options' => array(
			'' => t(''),
			'test3' => t('test3'),
		),
		'#required' => TRUE,
		'#description' => t('test3.'),
	);

	$form['hgui_manual_import']['hgui_textarea'] = array(
		'#type' => 'textarea',
		'#title' => t('Textarea'),
		'#rows' => 10,
		'#description' => t('test.')
	);
	
	$form['hgui_manual_import']['hgui_textarea_submit'] = array(
		'#type' => 'submit',
		'#value' => t('test'),
		'#submit' => array('hgui_manual_import_submit')
	);
	
	$form['hgui_manual_delete']= array(
		'#type' => 'textarea',
		'#title' => t('Textarea'),
		'#rows' => 10,
		'#description' => t('test.')
	);
	
	$form['hgui_manual_delete_submit'] = array(
		'#type' => 'submit',
		'#value' => t('test'),
		'#submit' => array('hgui_manual_delete_submit')
	);
	
	return $form;
}

Thank you for any help you can provide me.
Aaron

Comments

abovesun’s picture

The code you've just posted describes not two but one form with two submit buttons. What you need is separate method for each form, than you need put this forms on the page by drupal_get_form() method invocation for each form

Anarchtica’s picture

Thank you very much for your help. I was able to get this working correctly after you steered me in the right direction.

Hopefully this helps others who come across this post:

Here is my implementation of hook_menu():

$items['admin/content/hgui/manual'] = array(
 'title' => 'test',
 'page callback' => 'hgui_manual', // This calls the function that returns the two forms.
 'access callback' => 'user_access',
 'access arguments' => array('administer hgui'),
 'weight' => 3,
 'type' => MENU_LOCAL_TASK
 );

This is the hgui_manual function that was called in the above page callback:

function hgui_manual() {

	return drupal_get_form('hgui_manual_import') . drupal_get_form('hgui_manual_delete');
}

I then split the form code from my original post into two separate functions called 'hgui_manual_import' and 'hgui_manual_delete'.

So to summarize, my hook_menu page callback uses the hgui_manual function to display the content for this page. My hgui_manual function then grabs the two forms and displays them on the hgui_manual page. The URL is admin/content/hgui/manual as defined by hook_menu. Each form is then in it's own function and works correctly without the one form inheriting the required fields for the other form.

Thanks,
Aaron