简体   繁体   English

重建表单后将忽略drupal 7文本字段#default_value

[英]drupal 7 textfield #default_value ignored after rebuild a form

NOTE: I'm a drupal newbie. 注意:我是Drupal新手。 I'm developing a module based on the ajax examples: add_more and autocomplete. 我正在基于ajax示例开发模块:add_more和autocomplete。 The idea is to have a form where I can add visitors to a visit, persons being entered in the texfield populated by an LDAP connection using ajax. 我的想法是要有一个表单,我可以在其中添加访问者,访问者将被输入使用ajax在LDAP连接中填充的texfield中。 This works fine. 这很好。

The problem is that I want to be able to remove any visitor I want (not just the last one). 问题是我希望能够删除任何想要的访问者(而不仅仅是最后一个)。

I have a add visitor button that adds a fieldset containing the textfield for the visitor name and a suppress button (sorry, no image, my reputation is too low) for this visitor. 我有一个添加访问者按钮,它添加了一个字段集,其中包含访问者姓名的文本字段和一个抑制按钮(对不起,没有图像,我的声誉太低了)。 This means 3 visitors, 3 fieldsets with its 'suppress' button 这意味着3个访客和3个带有“抑制”按钮的字段集

When the user clicks on the suppress button of a visitor, I remove it, and call for a rebuild of the form but then, Drupal keeps displaying the first names as if I had suppressed the last one. 当用户单击访问者的“抑制”按钮时,我将其删除,并要求重建表单,但是随后,Drupal继续显示名字,就好像抑制了最后一个一样。

function gaz_edit_visit_form($form, &$form_state) {
    [...]
  // Build the fieldset with the proper number of names. We'll use
  // $form_state['num_names'] to determine the number of textfields to build.
  if (empty($form_state['num_names'])) {
    $form_state['num_names'] = 1;

    $form['visitors_fieldset']['name'][0] = array(
      '#type' => 'fieldset',
      '#title' => 'Visiteur 1'
    );

    $form['visitors_fieldset']['name'][0]['visitor'] = array(
      '#type' => 'textfield',
      '#title' => t('Nom du visiteur'),
      '#autocomplete_path' => 'gaz/visit/edit_visit_autocomplete_callback',
    );

    $form['visitors_fieldset']['add_name'] = array(
      '#type' => 'submit',
      '#value' => t('Ajouter un visiteur'),
      '#submit' => array('ajax_example_add_more_add_one2'),
      '#ajax' => array(
        'callback' => 'ajax_example_add_more_callback2',
        'wrapper' => 'visitors-fieldset-wrapper',
      ),
    );
  }
  else {
    for ($i = 0; $i < $form_state['num_names']; $i++) {
      $form['visitors_fieldset']['name'][$i] = array(
        '#type' => 'fieldset',
        '#title' => 'Visiteur ' . ($i + 1),
      );

      $form['visitors_fieldset']['name'][$i]['visitor'] = array(
        '#type' => 'textfield',
        '#title' => t('Nom du visiteur'),
        '#autocomplete_path' => 'gaz/visit/edit_visit_autocomplete_callback',
        '#default_value' => $form_state['values']['visitors_fieldset']['name'][$i]['visitor'],
      );

      if ($form_state['num_names'] > 1) {
        $form['visitors_fieldset']['name'][$i]['remove_visitor' . $i] = array(
          '#type' => 'submit',
          '#value' => t('Supprimer'),
          '#submit' => array('gaz_visitors_remove'),
          '#ajax' => array(
            'callback' => 'ajax_example_add_more_callback2',
            'wrapper' => 'visitors-fieldset-wrapper',
          ),
          '#gaz_visitor_index' => $i,
        );
      }

      $form['visitors_fieldset']['add_name'] = array(
        '#type' => 'submit',
        '#value' => t('Ajouter un visiteur'),
        '#submit' => array('ajax_example_add_more_add_one2'),
        '#ajax' => array(
          'callback' => 'ajax_example_add_more_callback2',
          'wrapper' => 'visitors-fieldset-wrapper',
        ),
      );
    }
  }

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

  return $form;
}

Here is the function that removes a particular user in the list. 这是在列表中删除特定用户的功能。

function gaz_visitors_remove($form, &$form_state) {
  array_splice($form_state['values']['visitors_fieldset']['name'], $form_state['triggering_element']['#gaz_visitor_index'], 1);
  if ($form_state['num_names'] > 1) {
    $form_state['num_names']--;
  }
  $form_state['rebuild'] = TRUE;
}

The $form at the end of the rebuild process (ie end of the gaz_edit_visit_form function) seems ok, including the default_values of the textfields but drupal fills them with other values. 重建过程结束时(即gaz_edit_visit_form函数结束)的$ form看起来不错,包括文本字段的default_values,但drupal用其他值填充它们。 If I have 3 visitors and then remove visitor 2, I expect to see names for 1 and 3 but I see names for 1 and 2. Again, $form... looks good and has no trace of user 2 anymore. 如果我有3位访问者,然后删​​除访问者2,则希望看到1和3的名称,但看到1和2的名称。同样,$ form ...看起来不错,并且不再有用户2的踪迹。

I guess I'm missing the huge elephant right before my eyes but I can't see it! 我想我在眼前想念那头巨大的大象,但我看不到它!

Try and go over this tutorial, https://drupal.org/node/717722 . 尝试阅读本教程https://drupal.org/node/717722 The way you created your form is wrong and that's why doesn't remove what you want. 您创建表单的方式是错误的,这就是为什么不删除您想要的内容。

When Drupal does an AJAX rebuild, its order of execution is: 当Drupal进行AJAX重建时,其执行顺序为:

  • build the form in the state that you submitted 以您提交的状态构建表单
  • validate form stuff 验证表单内容
  • execute stuff (call your #submit handler - I think) 执行内容(致电您的#submit处理程序-我认为)
  • rebuild the form for display 重建表格以显示
  • return the output (call your ajax #callback) 返回输出(调用您的ajax #callback)

$form_state['values'] is built in the first step. $ form_state ['values']在第一步中构建。 If you want to use it in the 4th step, as you do... then that's fine, but you need to keep this in mind. 如果您想像在第4步中那样使用它,那很好,但是您需要牢记这一点。

It explains why your deleted visitor data is still in there. 它说明了为什么您删除的访客数据仍然存在。 You have (num+1) visitors in $form_state['values'] 您有(num + 1)位访问者位于$ form_state ['values']

How to work around that? 如何解决? It depends how you want to change your code, that's up to you. 这取决于您要如何更改代码,这取决于您。 one way would be to have two counters, of which the second is the same as $i but at one point it can increment 1 extra, to skip the deleted value: (untested code) 一种方法是有两个计数器,第二个计数器与$ i相同,但是在某一点它可以增加1来跳过已删除的值:(未测试的代码)

$i2 = -1;
for (...) {
  $i2++;
  if (!empty($form_state['#triggering_element']) && $form_state['#triggering_element']['#value'] == t('Supprimer') && $form_state['#triggering_element']['#gaz_visitor_index'] == $i) {
    // OK, we pressed this button to delete a visitor, this one has been deleted so skip it.
    $i2++
  }
  ...
    '#default_value' => $form_state['values']['visitors_fieldset']['name'][$i2]['visitor']
  ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM