Hi there,

I have an exposed date filter with granularity Year/Month/Day, with Remember checkbox selected in the filter settings.

I get this error when any post back happens
An illegal choice has been detected. Please contact the site administrator.

Because the Year value goes away and become "-Year"

I tried to set the default year in the filter to 2009 but still acting the same.

Please help

CommentFileSizeAuthor
#26 date_filter_bug.zip5.71 KBbeeradb

Comments

alexku’s picture

Same problem. Subscribing ...

Maybe someone has an advise on how to change "-Year" to "somethingelse"? Thank You in advance :)

stella’s picture

Version: 6.x-2.1 » 6.x-2.x-dev

I have the same error with the latest version of the code, however it doesn't appear to be tied to the 'remember me' setting at all.

I have an exposed date select filter on the 'node: posted date' and have set the granularity to year and month. Regardless of what I configure the default date setting as the default options appear as '-Year' and 'Feb'. If I change the granularity to include all fields, then the default options are '-Year', 'Feb', '2', '2', '-Minute', '-Second' and the error is printed 3 times.

Applying the filters with these settings doesn't produce any errors, so I'm guessing that the problem lies in what these fields are set to by default when the view is first loaded. I don't mind investigating this further but I think I may need to be pointed in the right direction at this point.

stella’s picture

Title: Date exposed filter error when i select Remember. » Date exposed filter "illegal choice" error
stella’s picture

Oh in the log files I get the message:

Illegal choice 2 in date_filter element.
humble’s picture

Same for me. Subscribing.

humble’s picture

Anyone have any fresh insights on this problem?

iancawthorne’s picture

I'm also getting this problem. Subscribing.

FreddieK’s picture

I've run into this problem where I've had two views with the exact same settings, where one works and the other doesn't. If I delete the nonworking and then create a new clone with the same name, that one won't work eighter. I'd guess the problem is that some function isn't succesfully overwriting the -year setting in the database, but haven't looked into it deeper.

As for now, when working with date in views, I recommend having backups just in case...

denniemans’s picture

Same problem here...

klapper’s picture

I have two select boxes for a year from/to (in between) and the same problem.

I replaced the "Filter identifier" with an "x" instead of "date_filter". Messages are gone and filter works, even the default values are set now! Inserting no value will remove the filter boxes, but "x" works just fine.

iancawthorne’s picture

I can confirm that #10 worked for me too.

OliverColeman’s picture

Title: Date exposed filter "illegal choice" error » Date exposed filter "illegal choice" error when Remember enabled
Priority: Normal » Critical

It seems tied to the "Remember" option (though #2 says different!): the view was working fine until I set the Remember option, then I got the Illegal choice error. If the Filter identifier is changed AND the Remember option is disabled then the view starts working. If the Remember option is enabled again it breaks again (until disabling it and changing the Filter id again).

This is a pretty nasty bug; kinda permanently breaks the View (unless you come across this thread). Initially I tried removing and adding the filter again but that didn't fix it. Hope no one minds me changing it to critical priority!

narayanis’s picture

Confirmed that #10 worked for me as well. Changing the filter identifier makes the issue go away.

jason.fisher’s picture

#10 only works for me until I:

1) Make a new search (view driven by URL query string variables)
2) Browse back to that view (now is when the 'remembered' options are used)

jason.fisher’s picture

Title: Date exposed filter "illegal choice" error when Remember enabled » Date exposed filter "illegal choice" error when Remember enabled - Form API/Validation issue
Component: Miscellaneous » Date API
Status: Active » Needs work

Investigated the issue a bit .. no resolution yet, but some possibly helpful information:

Using an exposed and remembered field with granularity only on Year, Select elements on From and To using Between ..

Line 401 of date_api_elements.api, in the date_select_validate() function, this action seems suspicious:

function date_select_validate($element, &$form_state) {
  if (is_string($element['#value'])) {
    return;
  }
  
  ..

Dumping $element in two situations, one on the exposed form submit and one on revisiting a page with 'remember' options enabled for that same field, I see two different values:

Initial submit (working functionally):

 ..
    [#value] => Array
        (
            [year] => 2004
            [month] => 0
            [day] => 0
            [hour] => 0
            [minute] => 0
            [second] => 0
        )   
 ..

And after revisiting the page with remembered values ("Illegal choice" error):

 ..
  [#value] => 2004-01-01 00:00:00  
 ..

The value is being 'remembered' as a string, though it is being initially processed as an array. The array working makes sense given that we are in the date_select_validate() function.

This is the piece in the Drupal Form API (form.inc) that is catching us (line 709):

..

        elseif (!isset($options[$elements['#value']])) {
          form_error($elements, $t('An illegal choice has been detected. Please contact the site administrator.'));
          watchdog('form', 'Illegal choice %choice in %name element.', array('%choice' => $elements['#value'], '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $eleme$
        }

..

And $elements['#value'] is set to 2 for some reason -- when it should be 2004. However, $elements['#default_value'] is the correct value as 2004 -- which is the remembered value for the session.

Long story short .. I am still not certain where the correct fix for this is, but hopefully someone with more intimate knowledge of the module will find this information helpful.

Jason Fisher

nico24’s picture

I'm having the same problem here. #10 doesn't work for me. Anyone had a solution already?

I have a date filter (renamed identifier.) With the remember option on. First time I filter it with the date it works. But when I go to the next page I get an "An illegal choice has been detected. Please contact the site administrator." error. The problem seems to be as following: the date filter is an array, but the session saves it as a string. I debugged in get_exposed_input() in view.inc to get to know that.

So first of all in think this issue should be in Views. Secondly, anyone already has a solution..?

Thanks in advance!

jeffschuler’s picture

If the initially-set date year is in the 2000's, the error (upon using the remembered date) is:

Illegal choice 2 in date_filter element.

and the form controls (using day granularity...) look like:

-year Feb 2

If it's in 1000's (like 1999) the error is:

Illegal choice 1 in date_filter element.

and the form settings then look like:

-year Jan 1

...each field is trying to be set to the first character of the year. The year field is indexed by the actual year, and doesn't contain a "2" or "1" element... so, the FAPI error is thrown... whereas the other two fields do allow "1" or "2" as values -- which is why "Jan" or "Feb" shows up in the month drop-down, and "1" or "2" in the day...

Function docs for date_text_validate() (in date_elements_api.inc) say:

Before form submission $element['#value'] contains a string, after submission it contains an array.

Seems to corroborate what fysa said in #15: that Views remember is losing the array-formatted value of the date...

code-brighton’s picture

Not sure if this is the best way to go about this, but I fixed this issue by modifying views_handler_filter.inc, hopefully this will help someone anyway and/or be a pointer to a better solution:

I changed from the end of the function "store_exposed_input" from this:

       $session[$this->options['expose']['identifier']] = $input[$this->options['expose']['identifier']];
    }
  }

to this:

       if($this->options['field'] == 'date_filter'){
         $input[$this->options['expose']['identifier']]['min'] = $this->date_string_to_array($input[$this->options['expose']['identifier']]['min']);
         $input[$this->options['expose']['identifier']]['max'] = $this->date_string_to_array($input[$this->options['expose']['identifier']]['max']);
      }
      $session[$this->options['expose']['identifier']] = $input[$this->options['expose']['identifier']];
    }
  }

  /*
 * Added this function as form threw error on date filter:
 * An illegal choice has been detected. Please contact the site administrator.
 * This is because the session data is set to a string rather than array
 * so we do the conversion here. Might not be the right thing to do, maybe it's done
 * elsewhere but I think it will fix the issue
 */

function date_string_to_array($element){

    $split = explode(' ', $element);
    $date = $split[0];
    $time = $split[1];

    $split = explode('-', $date);
    $year = (int)$split[0];
    $month = (int)$split[1];
    $day = (int)$split[2] - 1;

    /*zero setting on month or year also threw illegal choice error */
    if($year==0){$year='';}
    if($month==0){$month='';}
    if($day < 0){$day = 0;}

    $split = explode('-', $time);
    $hour = (int)$split[0];
    $minute = (int)$split[1];
    $second = (int)$split[2];

    $a = array(
        'year' => $year,
        'day' => $day,
        'hour' => $hour,
        'minute' => $minute,
        'second' => $second,
        'month' => $month );

    return $a;
}
extexan’s picture

I have the same error... but not in Views at all. I'm using drupal_execute to run "user_register". There's a date field on my register form - defined via the Content Profile module. User/register works fine from the URL, but fails with the "Illegal choice 2 in Year element" when called by drupal_execute.

Neither the fix in #10 nor the one in #18 worked for me. Still hoping for a solution.

[Update: I fixed my error... it had to do with the fact that I was using the Date API to put a date field in an Ubercart checkout pane, then feeding the value into a CCK birthday Date field via drupal_execute("user_register"). I found that there was a difference in the way the #default_value was stored in those two cases.]

jeffschuler’s picture

I'm still looking at this in terms of Views filter with remember (sorry @ExTexan.)

To [re]-summarize my partial understanding of what's happening:

When the filter is normally (initially) submitted, the datetime string from the date select is transformed into an array, from which the select elements are properly assigned. However, when Views remember maintains filter values, this transformation doesn't happen, and each date select element is set to the first character of the datetime string. Form validation chokes because the Year select isn't indexed by integers (and, for example, can't be set to '2' -- the first character from '2010-08-...')

Interestingly, in date_parts_element() in date_api_elements.inc if you comment out lines 335 and 337:

<?php
	//if (!empty($element[$field]['#force_value'])) {
	  $sub_element[$field]['#value'] = $sub_element[$field]['#default_value'];
	//}
?>

...then the Views remember feature correctly maintains your chosen date values. This breaks the initial (normal) submit of the filter, though -- the chosen date isn't shown as maintained on first submit, (though it is "remember"ed, and will be shown when loading the same view page without URL params.)

This indicates to me that perhaps when Views re-submits the filter using remember, $element[$field]['#force_value'] isn't being set, (which it should be for 'exposed' filters.)

Hope this helps someone take this a step further...

karens’s picture

Status: Needs work » Fixed

I was finally able to reproduce this and find a fix. It was only affecting the select widget, finally turned out I needed an update in the way the select element is defined. None of the above patches was the right fix, the element needed a fix.

jeffschuler’s picture

Status: Fixed » Active

Thanks @KarenS: the error no longer appears and the Date is remembered in the select drop-downs when coming back to the view.

However, the remembered date doesn't seem to actually be used in the [Views] filtering...

beeradb’s picture

I just moved to the latest dev snaphot and am seeing the exact same issue. The view filter is 'remembered' in that it is auto selected, but the filter is not actually applied to the view.

beeradb’s picture

Further down the rabbit hole.......

I did some digging on this and noticed that the value property contained nulled out values on all page reloads after the initial form submissions.... this lead me to start poking around $view->exposed_data and I noticed the values were nulled out there as well.. so I started thinking perhaps the date handler was doing something funky and would need to override views_handler_filter::store_exposed_input(), as that appears to be where the input actually gets stored in session.

After digging around with that I'm actually just incredibly confused about what is happening. The date filter values DO get saved in session within views_handler_filter::store_exposed_input(), and are still there on the page reload, but the $input array that is being passed in contains only nulled out values. At that point the following line overwrites what is in $_SESSION:

$session[$this->options['expose']['identifier']] = $input[$this->options['expose']['identifier']];

I'm not sure why the values in session aren't going into $views->exposed_data for the date filter, as everything else in session for this view appears there. Any nudges in the right direction would be appreciated.

beeradb’s picture

Title: Date exposed filter "illegal choice" error when Remember enabled - Form API/Validation issue » Date exposed filter does not properly filter view when 'remember' is enabled.
beeradb’s picture

StatusFileSize
new5.71 KB

Here's a features export which demonstrates the issue. Just creates a content type w/ a date field, and a view which has an exposed filter for that date field. Just install the feature and generate some nodes of story type and you should be able to see the bug in action.

karens’s picture

Status: Active » Fixed

Took another stab at this. I think it is working now.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

alanburke’s picture

Category: bug » feature
Priority: Critical » Normal
Status: Closed (fixed) » Fixed

Hi Karen,
I can't find the individual commit for this fix - I've looked through the commit log, but nothing seemed obvious. Could you point it out.
Apologies for commenting on a closed issue.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.