I tried to hide a date_popup field using the drupal #states but I wasn't able to make any date_popup item invisible. It works fine with default drupal field types.

Comments

thomas4019’s picture

I had this same problem too. I worked around it by wrapping the date_popup field within a fieldset.

arlinsandbulte’s picture

Status: Active » Closed (won't fix)

Date 7.x-1.x is unsupported.
This issue should be verified in the latest version of Date (7.x-2.x-dev) and re-opened if still valid.

Thanks

jonlhouse’s picture

I'm experiences similar problems with #states not working even after installing date-7.x-2.x-dev. My code looks something like:

$element[$container]['options'] = array (
  '#type' => 'select',
  '#title' => t('Duration'),
  '#options' => array (
    'default' => t('Use default duration (2 weeks)'),
    'unlimited' => t('Polling remains open indefinetely'),
    'user_duration' => t('Let me choose duration'),
    'user_dates' => t('Let me choose start and end dates'),
  ),
  '#attributes' => array ('id' => 'vote_expiration_select',),
);
$element[$container]['popup_start'] = array (
  '#type' => 'date_popup',
  '#date_format' => 'm/d/Y',
  '#title' => t('Polling opens:'),
  '#states' => array (
    'visible' => array ('#vote_expiration_select' => array('value' => 'user_dates')),
  ),
);
$element[$container]['choose_duration'] = array (
  '#type' => 'select',
  '#title' => t('Duration'),
  '#default' => 14,
  '#options' => array (
    1 => 'One day', 7 => 'One week', 14 => 'Two weeks', 31 => 'One month', 365 => 'One year', 
  ),
  '#states' => array (
    'visible' => array ('#vote_expiration_select' => array('value' => 'user_duration')),
  ),
);

Basically if the user selects to choose their own date range (value: 'user_dates') the date_popup widget should appear. I included code for the select dropdown because the #states property functions properly with that widget. I didn't use the standard ":input[name=FORM_NAME]" since I've never gotten that to work properly so I assign a id instead and explicitly label the input.

Hope this helps. Thanks.

jesss’s picture

Version: 7.x-1.0-alpha2 » 7.x-2.x-dev
Status: Closed (won't fix) » Active

I have updated to 7.x.-2.x-dev and can confirm that #states does not appear to work with the date_popup field type -- or any of the other widgets.

I'm trying to use Date in conjunction with the Conditional Fields module, which controls the visibility of fields using the States API. The desired outcome is that when the event type is "repeat," this date field should be visible. (Note: This works as expected when switching a regular text field visible/invisible -- it's when the visible/invisible field is a date field that it breaks.)

When the form is rendered, the #states information is part of the form array, but it is not honored. Here's the output of the date field:

     [field_date_test] => array (
         [#type] => [container]
         [#attributes] => array (
             [class] => array (
                 [0] => [field-type-date]
                 [1] => [field-name-field-date-test]
                 [2] => [field-widget-date-popup]
             )
         )
         [#weight] => [0]
         [#tree] => [1]
         [#language] => [und]
         [und] => array (
             [0] => array (
                 [#entity_type] => [node]
                 [#bundle] => [calendar_event]
                 [#field_name] => [field_date_test]
                 [#language] => [und]
                 [#field_parents] => array (
                 )
                 [#columns] => array (
                     [0] => [value]
                 )
                 [#title] => [Date Test]
                 [#description] => []
                 [#required] => []
                 [#delta] => [0]
                 [#weight] => [0]
                 [#type] => [date_combo]
                 [#theme_wrappers] => array (
                     [0] => [date_combo]
                 )
                 [#default_value] => array (
                     [value] => []
                 )
                 [#date_timezone] => [America/New_York]
                 [#element_validate] => array (
                     [0] => [date_combo_validate]
                     [1] => [date_widget_validate]
                 )
                 [value] => array (
                     [#states] => array (
                         [visible] => array (
                             [[name="field_event_type[und]"]] => array (
                                 [value] => [repeat]
                             )
                         )
                     )
                 )
             )
slashrsm’s picture

I experienced the same error with form types.

karens’s picture

Status: Active » Fixed

I think the issue is that the date module creates a collection of fields instead of a single field. I am adding a wrapper around the collection to mimic what other fields look like and using states in some new code I will be adding to allow the user to hide/show the end date.

This will be committed shortly.

Status: Fixed » Closed (fixed)

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

ifux’s picture

Status: Closed (fixed) » Active

Sorry to reopen, but #states is still not working with date_popup field type.
visible and invisible works BUT for example enabled and disabled is not working!!!

Any ideas/help to fix or work around???

Thx
ifux

karens’s picture

Status: Active » Fixed

Please don't reopen a closed issue with a vague 'doesn't work' comment. You should provide a detailed description of what you tried that did not work. If you tried to apply 'disabled' at the top level of a collection of date fields, it will not work. That has nothing to do with #states, it won't work no matter how you do it, you can't disable a collection of fields. #disabled has to be applied to each individual form element to do anything. If that's the only issue here, this has nothing to do with #states and the issue is still fixed.

Status: Fixed » Closed (fixed)

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

liquidcms’s picture

Issue summary: View changes

won't re-open.. but i'll add my example usage in case anyone was like me and wasted a couple hours trying to figure this out.

i programmatically created an instance of a date_popup field for a custom node type in a custom module (i think things like datepicker_options and states should be definable when instance is being created; but couldnt figure that out so i did it with a form alter)

in my form alter i added this:

  $form['reservation_repeat_this_until']['#states'] = array(
    'visible' => array(
      ':input[name="reservation_repeat_type[und]"]' => array('value' => '2'),
    ),
  );

where 'reservation_repeat_type' is a radio buttons field. the part i missed was adding the [und] in to the states field selector.. and likely should be a 3rd level of quotes around the und i.e. ['und'] but i left it out and it worked.. but not good php practice.