如何在Drupal 7中创建多步自定义表单后退按钮?

时间:2017-04-25 20:39:57

标签: drupal drupal-7 drupal-modules drupal-views drupal-8

我通过2个步骤制作自定义表单,我想如果用户点击第2步表单上的后退按钮,则重定向到第1步并显示用户在开头输入的数据。这是我的代码

function example_form($form, &$form_state) {

  if (!empty($form_state['step_num'])) {
    return example_step_two($form, $form_state);
  }

  $form_state['step_num'] = 1;

  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#required' => TRUE,
  );

  $form['next'] = array(
    '#type' => 'submit',
    '#value' => 'Next',
    '#submit' => array('example_form_submit'),
  );

  return $form;
}

这是我的提交功能

function orders_order_form_submit($form, &$form_state) {

  $form_state['page_values']['1'] = $form_state['values'];

  if (!empty($form_state['page_values'][2])) {
    $form_state['values'] = $form_state['page_values'][2];
  }

  $form_state['step_number'] = 2;
  $form_state['rebuild'] = TRUE;
}

这是我表格的第二步

function example_form_step_two($form, &$form_state){

  $form['title2'] = array(
    '#type' => 'textfield',
    '#title' => t('Title2'),
    '#required' => TRUE,
  );

  $form['back'] = array(
    '#type' => 'submit',
    '#value' => t('Back'),
    '#submit' => array('example_form_step_two_back'),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
    '#submit' => array('example_form_step_two_submit'),
  );

  return $form;
}

这是后退按钮的问题功能

function example_form_step_two_back($form, &$form_state){

  $form_state['rebuild'] = TRUE;
}

1 个答案:

答案 0 :(得分:0)

看起来你需要测试'step_num'example_form()是否为2,而不是它是否为空:

if ($form_state['step_num'] == 2) {
    return example_step_two($form, $form_state);
}

然后在example_form_step_two_back()中,您需要添加:

$form_state['step_num'] = 1;

要显示先前用户输入的'title'值,您必须向其添加'#default_value'元素,如果该值存在,则设置该元素。