I get the error shown below while trying to implement dependent dropdown for country state as shown below
while trying to submit form which implements AJAX in drupal 8 for country - state dropdown I get the error
Error "An illegal choice has been detected. Please contact the site
administrator.". How can I make sure default value of State is retained like its retained for Country ?< br>
namespace Drupal\custom_changes\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\custom_changes\NameTable\NameTable;
use CommerceGuys\Addressing\Subdivision\SubdivisionRepository;
/**
* Simple Form
*
*/
class CustomDicForm extends FormBase {
/**
* @inheritdoc
*/
public function buildForm(array $form, FormStateInterface $form_state )
{
$form['country'] = [
'#type' => 'select',
'#title' => $this->t('Country'),
'#options' => [
'' => t('Select'),
'IN' => t('India'),
'GB' => t('UK (Great Britain)'),
],
'#ajax' => [
'callback' => '::stateCallback',
'event' => 'change',
'wrapper' => 'state-list',
'progress' => [
'type' => 'throbber',
'message' => $this->t('Updating States...'),
],
],
];
if (isset($form['#options'])) {
$form['state'] = [
'#type' => 'select',
'#prefix' => '<div id="state-list">',
'#suffix' => '</div>',
'#title' => $this->t('State'),
];
}
else {
$form['state'] = [
'#type' => 'select',
'#prefix' => '<div id="state-list">',
'#suffix' => '</div>',
'#title' => $this->t('State'),
'#options' => ['' => t('Select')],
];
}
$form['submit'] = [
'#type'=> 'submit',
'#value' => t('Submit'),
];
return $form;
}
public function stateCallback(array &$form, FormStateInterface $form_state) {
$country = $form_state->getValue('country');
$form_state->setValue('state', '');
$form['state']['#options'] = ['' => t('Select')] + $this->getAllStates(
$country
);
return $form['state'];
}
/**
*
* Get the State from Country
* @return array $states
*
*/
public function getAllStates($country) {
if ($country == "GB") {
// as GB does not give values so adding hard coded values
return ['A','B','C','D','E','F'];
}
else {
$subdivisionRepository = new SubdivisionRepository();
// Get the subdivisions for Brazil.
$states = $subdivisionRepository->getAll([$country]);
$in_states = array();
foreach ($states as $key => $state) {
$in_states[] = $key;
}
}
return $in_states;
}
/**
* @inheritdoc
*/
public function getFormId()
{
return 'custom_changes_dic_name_form';
}
/**
* @inheritdoc
*/
public function submitForm(array &$form, FormStateInterface $form_state)
{
parent::submitForm($form, $form_state);
$form['country']['#default_value'] = $form_state->getValue('country');
$form['state']['#options'] = ['' => 'Select'] + $this->getAllStates($form['country']['#default_value']);
$form['state']['#default_value'] = $form_state->getValue('state');
$first_name = $form_state->getValue('first_name');
$last_name = $form_state->getValue('last_name');
//@todo check how to pass exception
$this->nameTable->insertData($first_name, $last_name);
}
}