I have a form with only a select element and no submit button. Upon change, I issue an ajax call to retrieve the details of the selected Project. Everything works as expected, however after I leave the page, I see the error "An illegal choice has been detected. Please contact the site administrator."
I only get the error if I change the select input. Ex. if I navigate to:
Home->Projects->Home
I don't see the error on the last Home page. If i navigate to:
Home->Projects(change select input)->Home
I see the error on the last Home page.
Here is my module code:
class ExpeditionController extends ControllerBase {
public $client;
public function __construct() {
$this->client = \Drupal::service('http_client');
}
public function listProjects() {
$select_form = \Drupal::formBuilder()->getForm('Drupal\myModule\Form\ExpeditionSelectForm');
// Remove the submit button.
unset($select_form['actions']['submit']);
$result = $this->client->get('myRESTService', ['Accept' => 'application/json']);
$expeditions = json_decode($result->getBody());
foreach ($expeditions as $value) {
$select_form['expeditions']['#options'][$value->{'expedition'}->{'expeditionId'}] = $this->t($value->{'expedition'}->{'expeditionTitle'});
}
return array(
'#theme' => 'myModule_expeditions',
'#form' => $select_form,
);
}
public function expeditionDetail($form, $form_state) {
$expedition = [];
$response = new AjaxResponse();
if ($form['expeditions']['#value'] != "") {
$client = \Drupal::service('http_client');
$result = $client->get(
'myService' . $form['expeditions']['#value'],
['Accept' => 'application/json']
);
$expedition = json_decode($result->getBody());
}
$response->addCommand(new HtmlCommand(
'#expeditionDetail',
array(
'#theme' => 'myModule_expeditions_detail',
'#expedition' => $expedition,
)
));
return $response;
}
public function deploymentDetail($project_id, $deployment_id) {
$result = $this->client->get(
'myService' . $project_id . '/' . $deployment_id,
['Accept' => 'application/json']
);
return array(
'#theme' => 'myModule_deployment_detail',
'#deployment' => json_decode($result->getBody()),
);
}
}
class ExpeditionSelectForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'myModule_expeditions';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['expeditions'] = array(
'#type' => 'select',
'#title' => $this->t('Choose Project'),
'#empty_option' => $this->t('Select a project'),
'#default_value' => '',
'#ajax' => [
'callback' => 'Drupal\myModule\Controller\ExpeditionController::expeditionDetail',
],
);
return parent::buildForm($form, $form_state);
}
/**
* Gets the configuration names that will be editable.
*
* @return array
* An array of configuration object names that are editable if called in
* conjunction with the trait's config() method.
*/
protected function getEditableConfigNames() {
// TODO: Implement getEditableConfigNames() method.
return [];
}
}