0

I have an exposed filter form, and its default widgets are textfields. I'd like to change them to selects based on values from database. I have tried hook_form_alter and hook_form_views_exposed_form_alter, but get an error:

An illegal choice has been detected. Please contact the site administrator.

The code I used to change the widget is like this and I've tried #validate option.

$form['field_address_province'] = array(
        '#type' => 'select',
        '#options' => $province_options,
        '#validate' => true,
        '#description' => 'Select address',
    );

Edit:

Sorry for the lack of infomation. The $province_options is an array like

Array
(
    [0] => Please Select
    [6] => Area1
    [7] => Area2
    [2] => Area3
)
7
  • what is the return value of $province_options array?
    – xurshid29
    Dec 9, 2013 at 12:35
  • show the content inside the $province_options array.
    – Cool
    Dec 9, 2013 at 12:44
  • 1
    The first key must be empty string. Did you try to add options like this?: Array ( '' => Please Select //EMPTY STRING 6 => Area1 7 => Area2 2 => Area3 )
    – xurshid29
    Dec 9, 2013 at 13:21
  • 1
    i tested this kind of thing and found that when you try to switch a exposed widget field you need to provide an array with the empty first key, if it is checkboxes or radios, if new widget will be a select than you need to provide an array with the 'All' first key
    – xurshid29
    Dec 9, 2013 at 13:34
  • @xurshid29 Thank you, it works and the error disappeared. But '#default_value' option still doesn't work. Btw, is there any reference that show this "empty string" thing? This is the first time that I know empty string can be used as hash key in php...
    – leetom
    Dec 9, 2013 at 16:02

1 Answer 1

2

i tested this kind of thing and found that when you try to switch a exposed widget field you need to provide an array with the empty first key, if it is checkboxes or radios; if new widget will be a select then you need to provide an array with the 'All' first key. E.g.:

//Suppose that you have a default input widget which you want to change to select widget.
//We will get the values which we want our new select widget will get as options:
$opts = array(
  'first_key' => 'First value',
  'sec_key' => 'Second value',
);

// Then we will add required 'All' first key:
$options = array('All' => 'Choose a value') + $opts;

// Then we will create our new select widget
$form['YOUR_FIELD_NAME'] = array(
     '#type' => 'select',
     '#title' => t('MY TITLE'),
     '#options' => $options,
   );
4
  • It seems like 'All' still get the ERROR. I still have to use EMPTY STRING as key.
    – leetom
    Dec 11, 2013 at 12:45
  • @leetom I don't know why it is happening, but in my case select widget gets "All" as first option value, e.g.: <select id="edit-field-adv-mark-tid" class="form-select" name="field_adv_mark_tid" tabindex="-1"> <option class="" value="All">--Выберите</option> <option class="" value="438">Marka 1</option> <option class="" value="439">Marka 2</option> <option class="" value="1190">Marka 6</option> <option class="" value="1194">Marka 7</option> </select>
    – xurshid29
    Dec 11, 2013 at 12:54
  • @leetom if I change that value then I get that ERROR, like "An illegal choice has been detected. Please contact the site administrator."
    – xurshid29
    Dec 11, 2013 at 13:02
  • I wonder what's your views version. I am using views 3.7. I figured out that if I don't set the filter's value (first loading page), then the value is NULL, and NULL isn't in these options unless I add an option with EMPTY STRING as key (aka filter value). If I use 'All', '<Any>', or '-Any-' as the default option's key, I have to set filter's value to it in hook_init. I found this link
    – leetom
    Dec 12, 2013 at 3:33

Your Answer

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

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