DRUPAL 8-Ajax回调后无法更新单选按钮字段

问题描述 投票:0回答:1

我在Drupal 8中遇到了FORM的麻烦,想法是:

[从无线电台中选择一个无线电值来为无线电污染物充电,但是,在回调之后,我看到的结果为空,但是在我使用的replace命令中,我正确地看到了]]

这是buildForm的代码

public function buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state){

    $arrRadioContaminantes = array();
    $arrRadioContaminantes["0"] = t("ALL");

    //My function to access DB
    $result = \Drupal\map_data\Controller\Contaminante::list();
    foreach ($result as $it){
        $arrRadioContaminantes[$it["id_contaminante"]] = t($it["description"]);
    }

    $form['radio_contaminantes'] = array(
        '#type' => 'radios',
        '#options' => $arrRadioContaminantes,
        '#ajax' => [
            'callback' => '::seleccionContaminantes',
            'disable-refocus' => true,
            'event' => 'change',
            'wrapper' => 'radios-estaciones',
            'method' => 'replace'
        ]
    );

    $form['radio_estaciones'] = array(
        '#type' => 'radios',
        '#id' => 'radios-estaciones',
        '#options' => ['-1'=>t("SELECT CONTAMINANTE FIRST")]
    );

    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = array(
        '#type' => 'submit',
        '#value' => $this->t('Submit'),
        '#button_type' => 'primary',
    );

    $form_state->setCached(false);
    $form_state->setRebuild(true);

    return $form;
}

这是回调函数:

public function seleccionContaminantes(array &$form, FormStateInterface $form_state){
    if($form_state->getValue("radio_contaminantes") != 0){
        $form['radio_estaciones']["0"] = t("All");
    }

    //My Database function
    $resultado = \Drupal\montar_mapa\Controller\Estacion::list($form_state->getValue("radio_contaminantes"));
    foreach ($resultado as $it){
        $form['radio_estaciones'][$it["id"]] = t($it["name"]);
    }

    $ajaxResp = new AjaxResponse();
    $ajaxResp->addCommand(new \Drupal\Core\Ajax\ReplaceCommand('radios-estaciones', \Drupal::service('renderer')->render($form['radio_estaciones'])));

    return $ajaxResp;
}

我尝试返回具有相同结果的$ form ['radio_estaciones'],并尝试使用DataCommand粘贴新值,相同结果...我需要帮助

我在Drupal 8中的FORM上遇到麻烦,其想法是:从无线电连接处选择一个无线电值以为无线电污染源充电,但是,在回调之后,我看到的结果为空,但是在替换.. 。

php ajax drupal drupal-8
1个回答
0
投票

将类型更改为“选择”后效果很好。

© www.soinside.com 2019 - 2024. All rights reserved.