1

I am developing a custom module with the ability to store data on an external database using Drupal 7 Form API. I started with the multistep form example, added few more steps and other fields.

To implement the ability to edit those data I created a class that retrieves returns an array suitable to fill the $form_state['values'] element in order to have a default value for each form element. Here follows the code I have in the form callback when going to edit my data:

if( !empty($form_state['build_info']['args'][0]) ) ) {
    $a = new ind();
    $a->get_ind( $form_state['build_info']['args'][0] );

    $form_state['values'] = get_object_vars($a);
    unset($a);
}

Problem is when I hit the previous/next buttons because of the form is being rebuilt always with the data from the class so every change in a field is lost when hitting "Previous"; suddenly every change gets lost when I click "Next" after "Previous" (fields are empty!).

How do you achieve editing with multistep form with existing data? I am stuck here attempting to put the code above at submit and previous handlers with no success.

1 Answer 1

0

I think you should add if statement with #triggering_element or #clicked_button. Example:

function PREVIOUS_STEP_submission_OR_SOME_KIND_OF_CALLBACK($form, &$form_state) {
  if($form_state['clicked_button']['#value'] == 'BACK') {
    //show_previous_step()
  }
  else {
    $a = new ind();
    $a->get_ind( $form_state['build_info']['args'][0] );

    $form_state['values'] = get_object_vars($a);
    unset($a);
    // or your logic
  }
}
6
  • I tried this logic but I should fill default values only once (ie. the first time I open the form in edit mode), or I would loose any change. So, the workflow is: 1) open the form with the values fetched from the database 2) fill and/or edit the form with those default.
    – rmorelli74
    Commented Dec 13, 2013 at 11:29
  • @user18993 ok, let's imagine, I open the empty(new) form and start to fill the fields and I can continue to next step or go back to previous steps, right? in this case #clicked_button works fine, it restores values from somekind of temporary storage. But when I finish the form, I want to change something and I open it and try to move by steps, here problems begin, right?
    – xurshid29
    Commented Dec 13, 2013 at 11:45
  • ok, step2: the form is filled with my defaults step3: form is filled with default but i go back to step2 and... previously made changes are lost, moreover if I go again to step3, fields are blank. That's the situation.
    – rmorelli74
    Commented Dec 13, 2013 at 11:57
  • @user18993 how did you create your multistep form? Here is a another way to create MS form: davidsonj.com/blog/how-create-multi-step-form-drupal-7
    – xurshid29
    Commented Dec 13, 2013 at 12:02
  • I started with the drupal7 form example module, form_example_wizard.inc file, then added other steps and validation functions.
    – rmorelli74
    Commented Dec 13, 2013 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.