【问题标题】:Drupal 7 date_popup form element wrong value submittedDrupal 7 date_popup 表单元素提交的值错误
【发布时间】:2019-11-10 14:08:41
【问题描述】:

我正在使用 Drupal 7 在自定义表单中呈现以下日期选择器元素:

    $form['navigation']['calendar_popup'] = array(
        '#type' => 'date_popup',
        '#title' => t('Date', [], $t_context),
        '#title_display' => 'invisible',
        '#default_value' => date('Y-m-d H:i:s', time()),
        '#date_format' => 'd-m-Y',
        '#size' => 10,
        '#date_label_position' => 'none', // none within
        '#date_increment' => 15,
        '#date_year_range' => '2008:+1',
        '#description' => '',
        '#required' => false,      
    );

今天是 11 月 10 日,显示的元素文本是 10-11-2019。 但是,当在手持设备上提交表单时(我在 Chrome 桌面浏览器中使用三星 Galaxy S5 仿真),字段文本会短暂更改为“11-10-2019”,实际上“2019-10-11”被提交。在物理 Galaxy A 系列手持设备上也是如此)。

桌面浏览器没有这个问题。

有什么建议吗?

【问题讨论】:

    标签: drupal-7 drupal-fapi


    【解决方案1】:

    试试这个:

     $form['navigation']['calendar_popup'] = array(
        '#type' => 'date_popup',
        '#title' => t('Date', [], $t_context),
        '#title_display' => 'invisible',
        '#default_value' => date('Y-m-d', time()), // change format passed
        '#date_format' => 'd-m-Y',
        '#size' => 10,
        '#date_label_position' => 'none', // none within
        '#date_increment' => 15,
    
        '#date_timezone'       => date_default_timezone(), // add this
    
        '#date_year_range' => '2008:+1',
        '#description' => '',
        '#required' => false,      
    );
    

    => 强制时区 + 更改日期默认值格式

    【讨论】:

    • 不幸的是没有运气。该字段今天(11 月 12 日)呈现,其中包含文本 12/11/2019。但是在我的三星 Galaxy 上使用 Chrome 时,$form_state['values']['calendar_popup'] 提交的值仍然是 '2019-12-11'。 Win10下运行的桌面Chrome是可以的。我怀疑 Date Popup 模块附带的 JQuery timepicker 插件有问题。将尝试 wvega。
    • 你可以尝试使用php.net/manual/fr/function.date-default-timezone-set.php强制你的后端使用正确的UTC
    • @PavelMurnikov 你试过了吗?
    • 是的,我确实尝试过您的表单元素建议。现在它正在工作。后来我做了一些额外的更新,这仍然需要弄清楚。将相应地发布我的问题的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多