1

Can we call submit handler of one form into another form?

I have two forms say user profile form and choose membership plan I want to unset submit function of choose membership plan and want to show in user profile form and also both forms field must save.

like multi-step form.

 function myform_form_alter(&$form, &$form_state, $form_id) {
   if($form_id === "ms_membership_list_memberships_form") {
     $form['#validate'][] = "_myform_form_submit_handler";
   }
   if($form_id === "user_profile_form") {
     $form['#validate'][] = "_myform_form2_submit_handler";
   }
 }

 function _myform_form_submit_handler($form, &$form_state) {
   $form_state['rebuild'] = TRUE;
   $form_state['redirect'] = '/user/1/edit';
 }

 function _myform_form2_submit_handler($form, &$form_state) {
   $form_state['rebuild'] = TRUE;
   $form_state['redirect'] = '/ms/checkout';
 }
0

You could pass the page number as argument to your hook_menu()

<?php    
function mymodule_menu() {
    $items['abc/def/%'] = array(
      'page callback' => 'mymodule_abc_view',
      'page arguments' => array(2),
    );
    return $items;
  }

Then if it is step-one for example, get your subscription plan form, render it then submit. In the submit function use:

 <?php
// Force the form to rebuild. If we do not do this, the value(s) in $form_state['storage'] will be lost
$form_state['rebuild'] = TRUE;
//Redirect to step two
$form_state['redirect'] = 'abc/def/2';

In step 2 get the user profile so that in the next submission you'll have the data from both forms in the submit function.

  • $form_state['redirect'] not redirecting the page in submit function – user59414 Jun 8 '16 at 9:44
  • Have you passed &$form_state as reference to the submit handler? – Ismail Cherri Jun 8 '16 at 19:59
  • yes i passed it to submit handler ,my problem is page is redirecting to the same page – user59414 Jun 9 '16 at 4:32
  • i have added the code above where i am doing wrong – user59414 Jun 9 '16 at 5:05
  • That's the point exactly, you need to call drupal_get_form() to get the user form on the second page. You could also add the page number as a value to the $form_state variable to know which page the user is currently on – Ismail Cherri Jun 9 '16 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy