Hi All,

I have been trying for the last week or so to design a question form that will request data and then based on that selection pass on to another form. I have seen the 10 stage tutorial on forms(http://drupal.org/node/262422), Code sample #10, and gave that I try. It doesn't work for me though. Not sure what I have doing wrong. I understand how that you test for stage two and if not it will then generate the first form, once you click next it will set a veriable in the storage array to TRUE and then save the array for future use. What I don't get is where is the program told to rerun the form where it does the check to see if it needs to generate the second form. Mine never gets to there. It always just renders the first form.

Here is all my code that deals with this :

 function aonline_province_add_form()
{	
	//print_r($form_state);
	
	if (isset($form_state['storage']['page_two']))
	{
		return aonline_province_add_page_two();		
	}

	$form['e_details'] = array(
		'#type' => 'fieldset',
		'#title' => t('Select Country'),
		'#collapsible' => TRUE,
		'#collapsed' => FALSE,
		'#tree' => TRUE,
		'#description' => "Please select the country you would like to add the Province",
	);
	
	$form['e_details']['e_name'] = array(
		'#type' => 'textfield',
		'#title' => t('Establishment Name'),
		'#size' => 30,
		'#maxlength' => 64,
		'#description' => t('Enter the name for this group of settings'),
	);
	
	$form['clear'] = array(
		'#type' => 'submit',
		'#value' => 'Reset form',
		'#validate' => array('my_module_my_form_clear'),
	);
	
	$form['next'] = array(
		'#type' => 'submit',
		'#value' => 'Next >>',
	);
	
	return $form;
}

// New function created to help make the code more manageable
function aonline_province_add_page_two() 
{
	$form['color'] = array(
		'#type' => 'textfield',
		'#title' => 'Favorite color',
	);
 
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => 'Submit',
	);
	return $form;
}

function aonline_province_add_form_clear($form, &$form_state) 
{
	unset ($form_state['values']); // ensures our fields are blank after reset
                                 // button is clicked
	$form_state['rebuild'] = TRUE;
}

function aonline_province_add_form_submit($form, &$form_state)
{
	//print_r($form_state);
	
	if ($form_state['clicked_button']['#id'] == 'edit-next')
	{
		//print_r($form_state);
		$form_state['storage']['page_two'] = TRUE;
		$form_state['storage']['page_one_values'] = $form_state['values'];
	}
	// Handle page 2 submissions
	else 
	{
		drupal_set_message('Your form has been submitted');
		unset ($form_state['storage']); // This value must be unset or we will

		$form_state['redirect'] = 'node'; // Here's how we redirect the user.
	}
}

function aonline_province_add_page() 
{
	return drupal_get_form('aonline_province_add_form');
}

Anyone know what the mechanism is that tells drupal to rerun the form function?

Thanks in advance.

Davin.

Comments

Trappies’s picture

Hi again,

Looked a bit further and cannot see how it gets to the next stage. Anyone know of anymore multi stage forms that I can compare?

Thanks in advance.

Davin

Trappies’s picture

Ah,

Been doing a few outputs to see where the veriables go... I see that the values do get sent through to the submit function which is fine. The $form_state['storage']['page_two'] value also does get set. When it calls the below function for the second time :

function aonline_province_add_form()
{	
	print_r($form_state['storage']);
	
	if (isset($form_state['storage']['page_two']))
	{
		return aonline_province_add_page_two();		
	}

	$form['e_details'] = array(
		'#type' => 'fieldset',
		'#title' => t('Select Country'),
		'#collapsible' => TRUE,
		'#collapsed' => FALSE,
		'#tree' => TRUE,
		'#description' => "Please select the country you would like to add the Province",
	);
	
	$form['e_details']['e_name'] = array(
		'#type' => 'textfield',
		'#title' => t('Establishment Name'),
		'#size' => 30,
		'#maxlength' => 64,
		'#description' => t('Enter the name for this group of settings'),
	);
	
	$form['clear'] = array(
		'#type' => 'submit',
		'#value' => 'Reset form',
		'#validate' => array('my_module_my_form_clear'),
	);
	
	$form['next'] = array(
		'#type' => 'submit',
		'#value' => 'Next >>',
	);
	
	return $form;
}

the $form_state['storage'] array is blank. So why would it be blank? Isn't it supposed to be available on a global level or am I not putting in a required statement to make it global? This is the reason why it keeps on failing and showing the first form.

Any ideas why?

Thanks in advance.

Davin

Trappies’s picture

Hi,

Found the error in my ways :P I wasn't passing the $form_state as a parameter to the form. It is now working :)

Davin.