Php Drupal 8-提交后不重新生成Ajax表单
我对Drupal8中使用Ajax的自定义表单有一个问题 让我解释一下。我有两个select字段,当您在第一个字段中选择一个选项时,Ajax请求将获得第二个字段的选项。当您提交表单时,它会将您重定向到特定页面。所有这些都很有效 问题是当你重定向到一个页面时,你点击了浏览器的“上一页”按钮。默认情况下,您会得到第一个字段的第一个选项,第二个字段的第一个选项,但在这种特定情况下,您会得到之前为第一个字段选择的选项,但第二个选择不会得到好的值,因为Ajax请求不会被激发Php Drupal 8-提交后不重新生成Ajax表单,php,ajax,forms,drupal,drupal-8,Php,Ajax,Forms,Drupal,Drupal 8,我对Drupal8中使用Ajax的自定义表单有一个问题 让我解释一下。我有两个select字段,当您在第一个字段中选择一个选项时,Ajax请求将获得第二个字段的选项。当您提交表单时,它会将您重定向到特定页面。所有这些都很有效 问题是当你重定向到一个页面时,你点击了浏览器的“上一页”按钮。默认情况下,您会得到第一个字段的第一个选项,第二个字段的第一个选项,但在这种特定情况下,您会得到之前为第一个字段选择的选项,但第二个选择不会得到好的值,因为Ajax请求不会被激发 示例: 当您在第一个字段中选择
示例: 当您在第一个字段中选择“选项A”时,您将在第二个字段中获得“值A.1”和“值A.2”;如果您选择“选项B”,您将获得“值B.1”,等等 例如,如果您提交带有“Option C”和“Value C.3”的表单,您将被重定向到一个页面,如果您返回到浏览器中的上一个页面,您将选择“Option C”,但在第二个字段(“Value a.1”、“Value a.2”…)中带有“Option a”的值,如果您提交,则会出现此错误 检测到非法选择。请联系该网站 管理员
我当然错过了重建表单的过程,但我找不到任何解决方案 这是我的密码:
<?php
namespace Drupal\chdou_intuitive_search\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
/**
* Class DefaultForm.
*
* @package Drupal\chdou_intuitive_search\Form
*/
class DefaultForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'default_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['i_wish'] = [
'#type' => 'select',
'#title' => $this->t('Je souhaite'),
'#options' => [
'appointment' => $this->t('prendre rendez-vous'),
'doctor' => $this->t('rechercher un médecin'),
'help' => $this->t('une aide ou un soutien')
],
'#default_value' => 'appointment',
'#required' => true,
'#multiple' => false,
'#size' => false,
'#ajax' => [
'callback' => '::getOptions',
'event' => 'change',
'wrapper' => 'what',
]
];
$parent = $form_state->getValue('i_wish');
if (empty($parent)) {
$parent = $form['i_wish']['#default_value'];
}
$form['what'] = [
'#prefix' => '<div id="what">',
'#suffix' => '</div>',
'#type' => 'select',
'#title' => $whatLabel,
'#options' => $this->getOptionsBySelection($parent),
'#default_value' => key($this->getOptionsBySelection($parent)),
'#required' => true,
'#multiple' => false,
'#size' => false,
];
$form['submit'] = [
'#prefix' => '<div class="form-actions">',
'#suffix' => '</div>',
'#type' => 'submit',
'#value' => $this->t('Search'),
'#attributes' => [
'class' => [
'button',
],
],
];
return $form;
}
/**
* Ajax callback for the "Je souhaite" dropdown
*/
public function getOptions(array &$form, FormStateInterface $form_state) {
return $form['what'];
}
/**
* Returns list of options that correspond to the "Je souhaite" selected option
*/
public function getOptionsBySelection($parent) {
switch ($parent) {
case 'appointment':
$terms = $this->getAppointmentOptions();
break;
case 'doctor':
$terms = $this->getDoctorOptions();
break;
case 'help':
$terms = array(
'140' => 'Je suis patient',
'141' => 'Je suis aidant',
'142' => 'Pour un proche',
);
break;
default:
break;
}
return $terms;
}
/**
* Returns list of care_sheet_consultation taxonomy tree
*/
public function getAppointmentOptions() {
// ...
}
/**
* Returns list of :
* - care_sheet_consultation
* - care_sheet_hospitalization
* - care_sheet_medical_techno
* taxonomy trees
*/
public function getDoctorOptions() {
// ...
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// CHOIX : Je souhaite prendre rendez-vous
if ($form_state->getValue('i_wish') == 'appointment') {
$form_state->setRedirect('entity.taxonomy_term.canonical', [
'taxonomy_term' => $form_state->getValue('what')
]);
}
// CHOIX : Je souhaite rechercher un médecin
else if ($form_state->getValue('i_wish') == 'doctor') {
$form_state->setRedirect('entity.node.canonical', [
'node' => '29', // i.e. page Annuaire
'service' => $form_state->getValue('what')
]);
}
// CHOIX : Je souhaite une aide ou un soutien
else if ($form_state->getValue('i_wish') == 'help') {
$form_state->setRedirect('entity.node.canonical', [
'node' => $form_state->getValue('what')
]);
}
}
}
我设法让它并行工作。遵循以下线程: