I am building a form that can either begin on the home page or at the ‘/apply’ url. I used this tutorial to build the multi-step form. The form works great if I start from ‘/apply’ and begin at the very beginning of the form process. The form does not work if I start from the home page and get redirected to the second step of the form, then try to submit that second step. This is the error message I receive when I try to submit that second page of the form when I started on the home page:
PHP Fatal error: Cannot create references to/from string offsets nor overloaded objects in /Applications/MAMP/htdocs/drupal/includes/common.inc on line 6548 PHP Stack trace: PHP 1. {main}() /Applications/MAMP/htdocs/drupal/index.php:0 PHP 2. menu_execute_active_handler() /Applications/MAMP/htdocs/drupal/index.php:21 PHP 3. call_user_func_array() /Applications/MAMP/htdocs/drupal/includes/menu.inc:517 PHP 4. drupal_get_form() /Applications/MAMP/htdocs/drupal/includes/menu.inc:517 PHP 5. drupal_build_form() /Applications/MAMP/htdocs/drupal/includes/form.inc:131 PHP 6. drupal_process_form() /Applications/MAMP/htdocs/drupal/includes/form.inc:374 PHP 7. form_builder() /Applications/MAMP/htdocs/drupal/includes/form.inc:842 PHP 8. form_builder() /Applications/MAMP/htdocs/drupal/includes/form.inc:1857 PHP 9. form_builder() /Applications/MAMP/htdocs/drupal/includes/form.inc:1857 PHP 10. form_builder() /Applications/MAMP/htdocs/drupal/includes/form.inc:1857 PHP 11. _form_builder_handle_input_element() /Applications/MAMP/htdocs/drupal/includes/form.inc:1795 PHP 12. drupal_array_set_nested_value() /Applications/MAMP/htdocs/drupal/includes/form.inc:2003
I am attempting to continue the multi-step form from the home page to the ‘/apply’ page by saving the $form_state['storage']
array in the session, then reading it out of the session in the custom_module_primary_form
function.
//Save form 'storage' in $_SESSION function custom_module_first_form_submit($form, &$form_state) { ... if(request_path() == '') { $form_state['redirect'] = array('apply'); $_SESSION['storage'] = $form_state['storage']; $form_state['rebuild'] = FALSE; } }
This is the modified custom_module_primary_form
function from the tutorial.
//Read form 'storage' out of $_SESSION function custom_module_primary_form($form, &$form_state) { // Check to see if anything has been stored. if ($form_state['rebuild']) { $form_state['input'] = array(); } if (empty($form_state['storage'])) { if(isset($_SESSION['storage'])) { $form_state['submitted'] = TRUE; $form_state['rebuild'] = TRUE; $form_state['storage'] = $_SESSION['storage']; unset($_SESSION['storage']); } else { // No step has been set so start with the first. $form_state['storage'] = array( 'step' => 'custom_module_first_form', ); } } // Return the current form $function = $form_state['storage']['step']; $form = $function($form, $form_state); return $form; }