At the user account form, I've created two select fields (of type list_string) for state and city.

The state field has a list of states for its allowed values config, and the city field has no values for its allowed values config (cause its supposed to have dynamic values).

The city field is being populated dynamically via ajax after selecting a state (it's a dependent dropdown mechanism).

It was all fine until I tried to save the entity and got an "Illegal choice selected..." error message.

I've tried all (decent) workarounds for this problem but none of them worked (including a dynamic values list).

For those wondering, this is how I implemented the dynamic values list when I tried it:

 * Set dynamic allowed values for the city field.
 * @param \Drupal\field\Entity\FieldStorageConfig $definition
 *   The field definition.
 * @param \Drupal\Core\Entity\ContentEntityInterface|null $entity
 *   The entity being created if applicable.
 * @param bool $cacheable
 *   Boolean indicating if the results are cacheable.
 * @return array
 *   An array of possible key and value options.
 * @see options_allowed_values()
function module_name_cities_allowed_values(FieldStorageConfig $definition, ContentEntityInterface $entity = NULL, bool $cacheable): array {
  $options = [];

  if ($entity->bundle() == 'user') {
    $state = $entity->getState(); // Returns the value for `field_user_state`

    $cities = module_name_load_cities_from_state($state);
    $options = array_merge(['_none' => '- Select a city -'], $cities);

  return $options;

As you can see it wouldn't work, since I am relying on the entity being filled with values by the time I try to load the list of cities from an another entity field (state). Is that clear?

In the ideal world, I'd be able to remove this "valid choice" validation, that is set through the AllowedValues constraint at some point. Unfortunately, I had no success trying to do it.

Does anyone know how to remove this validation constraint?

I can see it is set by inspecting the return value of \Drupal::entityManager()->getStorage('field_config')->load('user.user.field_user_city')->getItemDefinition()->getConstraints();

How to bypass this allowed values validation?

  • How exactly did you implement the dynamic allowed values callback?
    – leymannx
    Aug 5, 2019 at 19:43
  • Possible duplicate of Dynamic allowed values list
    – leymannx
    Aug 5, 2019 at 19:44
  • @leymannx I've edited the question to include an example of how I tried to implement a dynamic allowed_values_function and why I don't believe that to be a valid solution for this question. Thank you. Aug 5, 2019 at 20:04
  • I've had the exact same situation before and invested a lot of time in finding a solution. The way I solved it in the end was to programmatically allow all possible values for the dependent field (in your case city). Ajax will still populate this field only with the cities of the selected state, but no matter what state is selected, all cities are allowed.
    – theuni
    Aug 8, 2019 at 6:40


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.