【问题标题】:"An illegal choice is detected..." error with dynamic dropdown select list I Drupal8“检测到非法选择...”动态下拉选择列表错误 I Drupal8
【发布时间】:2020-06-25 00:34:50
【问题描述】:

我为 hook_form_alter 中的动态下拉选择列表编写了这段代码。选项由外部数据库填充。

function car2db_annuncio_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'node_annuncio_form') {
    $options_type = car2db_annuncio_type_dropdown_options();
    $form['field_marca']['#prefix'] = '<div id="field_marca">';
    $form['field_marca']['#suffix'] = '</div>';
    $form['field_tipologia']['widget']['#options'] = $options_type;
    $form['field_tipologia']['widget']['#ajax'] = array(
      'event' => 'change',
      'callback' => 'car2db_annuncio_make_ajax_callback',
      'wrapper' => 'field_marca',
      'disable-refocus' => FALSE,
      'progress' => [
        'type' => 'throbber',
        'message' => t('Verify...'),
      ]
    );
  }
}

function car2db_annuncio_type_dropdown_options() {
  $connection = Database::getConnection('default', 'migrate');

  $dropdown_type = ['none' => '- Seleziona - '];
  $sql_type = "SELECT * FROM `car_type`";
  $query_type = $connection->query($sql_type);
  $res_type = $query_type->fetchAll();
  foreach ($res_type as $row){
    $key = $row->id_car_type;
    $value = $row->name;

    $dropdown_type[$key] = $value;
  }
  return $dropdown_type;
}

function car2db_annuncio_make_dropdown_options($key_type) {
  $connection = Database::getConnection('default', 'migrate');

  $dropdown_make = ['none' => '- Seleziona - '];
    $sql_make = "SELECT * FROM `car_make` WHERE `id_car_type` = :tipo";
    $query_make = $connection->query($sql_make, [':tipo' => $key_type]);
    $res_make = $query_make->fetchAll();
    foreach ($res_make as $row){
      $Key_make = $row->id_car_make;
      $Make_value = $row->name;

      $dropdown_make[$Key_make] = $Make_value;
    }

  return $dropdown_make;
}

function car2db_annuncio_make_ajax_callback(array &$form, FormStateInterface $form_state) {
  if ($selectedValue = $form_state->getValue('field_tipologia')) {
    $selectedValue = (int) $selectedValue[0]['value'] ? (int) $selectedValue[0]['value'] : 0;
    $options_marca = car2db_annuncio_make_dropdown_options($selectedValue);
    $form['field_marca']['widget']['#options'] = $options_marca;
  }
  return $form['field_marca'];
}

现在,当点击“保存按钮”时,总是出现“检测到非法选择....”错误。 我还尝试将选项加载到 hook_form 更改中,但它总是返回错误。 我哪里错了?

【问题讨论】:

    标签: ajax callback drupal-8


    【解决方案1】:

    我记得当我第一次开始在 Drupal 中使用 ajax 表单时,这让我非常困惑。
    希望我没记错。
    基本上,您必须将用于将动态选项添加到表单构建函数中的逻辑移动(但在您的情况下,它是 alter 函数)。
    调用 ajax 函数时,仍会重建表单并调用您的 alter 函数(并具有当前的 form_state)。
    您的 ajax 函数只会返回新的表单元素。
    类似下面的东西(未经测试)

    function car2db_annuncio_form_alter(&$form, FormStateInterface $form_state, $form_id) {
      if ($form_id == 'node_annuncio_form') {
        $options_type = car2db_annuncio_type_dropdown_options();
        $form['field_marca']['#prefix'] = '<div id="field_marca">';
        $form['field_marca']['#suffix'] = '</div>';
        $form['field_tipologia']['widget']['#options'] = $options_type;
        $form['field_tipologia']['widget']['#ajax'] = array(
          'event' => 'change',
          'callback' => 'car2db_annuncio_make_ajax_callback',
          'wrapper' => 'field_marca',
          'disable-refocus' => FALSE,
          'progress' => [
            'type' => 'throbber',
            'message' => t('Verify...'),
          ]
        );
        // Check selected value here.
        if ($selectedValue = $form_state->getValue('field_tipologia')) {
          $selectedValue = (int) $selectedValue[0]['value'] ? (int) $selectedValue[0]['value'] : 0;
          $options_marca = car2db_annuncio_make_dropdown_options($selectedValue);
          $form['field_marca']['widget']['#options'] = $options_marca;
        }
      }
    }
    
    function car2db_annuncio_type_dropdown_options() {
      $connection = Database::getConnection('default', 'migrate');
    
      $dropdown_type = ['none' => '- Seleziona - '];
      $sql_type = "SELECT * FROM `car_type`";
      $query_type = $connection->query($sql_type);
      $res_type = $query_type->fetchAll();
      foreach ($res_type as $row){
        $key = $row->id_car_type;
        $value = $row->name;
    
        $dropdown_type[$key] = $value;
      }
      return $dropdown_type;
    }
    
    function car2db_annuncio_make_dropdown_options($key_type) {
      $connection = Database::getConnection('default', 'migrate');
    
      $dropdown_make = ['none' => '- Seleziona - '];
        $sql_make = "SELECT * FROM `car_make` WHERE `id_car_type` = :tipo";
        $query_make = $connection->query($sql_make, [':tipo' => $key_type]);
        $res_make = $query_make->fetchAll();
        foreach ($res_make as $row){
          $Key_make = $row->id_car_make;
          $Make_value = $row->name;
    
          $dropdown_make[$Key_make] = $Make_value;
        }
    
      return $dropdown_make;
    }
    
    function car2db_annuncio_make_ajax_callback(array &$form, FormStateInterface $form_state) {
      // Just return the form element (that has been rebuilt in the form_alter)
      return $form['field_marca'];
    }
    

    【讨论】:

    • Nothing...now 错误是:“您选择的值不是有效的选择。”
    猜你喜欢
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 2022-07-21
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多