0

I'm trying to transform my textinput into a select-field but I keep stumbling upon An illegal choice has been detected-error.

What I try in my hook_form_alteris this:

$form['distance']['search_distance']['#type'] = 'select';
$form['distance']['search_distance']['#options'] = drupal_map_assoc(array(100 => '100 km', 200 => '200 km'));

It looks like he's doesn't like strings to be in the select list, although, the value of the input is an integer...

How can I make this input field accept my modifications?

1

I don't know if this is causing the error, but you're using drupal_map_assoc() wrong...

The point of that function is to take a linear array and convert it to an associative array with the values of the given array as associated keys. e.g.

$form['distance']['search_distance']['#options'] = drupal_map_assoc(array(100, 200, 300));

I kind of suspect what you're meaning to do is this:

$form['distance']['search_distance']['#options'] = array(100 => '100 km', 200 => '200 km');

Your array is already associative, no need to try making it associative all over again :)

  • For some reason, your answer is working, but when I change it to array(5 => '5 km', 10 => '10 km', 20 => '20 km', 50 => '50 km');, I get the illegal choice again... – Michiel Feb 18 '13 at 15:30
  • Strange...might have something to do with the fact the values are integers in the first example, and strings in the second but I'm not sure (can't think of any other reasonable difference) - I don't think what you're doing is exactly 'supported' as it were :P – Clive Feb 18 '13 at 15:31
  • Ok, apparently, the initial value of the dropdown was "100", that's why it didn't gave an error with your value and it did with mine. But it's solved now :) – Michiel Feb 19 '13 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.