DRUPAL 8 - ajax 回调后无法更新单选按钮字段
DRUPAL 8 - Can't update radio button fields after ajax callback
我在使用 Drupal 8 中的 FORM 时遇到问题,想法是:
从radio-estaciones中选择了一个radio value来给radio-contaminantes充电,但是,回调之后,我看到的结果是空的,但是在我使用的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 传递新值,结果相同...我需要帮助
将类型更改为 'select' 后工作正常。
Drupal 有这个错误
https://www.drupal.org/project/drupal/issues/2758631
复选框情况相同
我在使用 Drupal 8 中的 FORM 时遇到问题,想法是:
从radio-estaciones中选择了一个radio value来给radio-contaminantes充电,但是,回调之后,我看到的结果是空的,但是在我使用的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 传递新值,结果相同...我需要帮助
将类型更改为 'select' 后工作正常。
Drupal 有这个错误
https://www.drupal.org/project/drupal/issues/2758631
复选框情况相同