【问题标题】:display default time in cakephp ctp在 cakephp ctp 中显示默认时间
【发布时间】:2013-03-11 19:19:39
【问题描述】:

我希望在 ctp 时间下拉菜单中将默认时间显示为上午 9:00。以下是我的 ctp 代码:

<?php
                                      echo $this->Form->input('Rideoffer.DepartureTime', array(
                                                        'type' => 'time',

                                                         'interval' => 5
                                                         ));
                             ?>

我该怎么做?

【问题讨论】:

    标签: cakephp time cakephp-2.0


    【解决方案1】:

    使用“选择”选项

    <?php
    echo $this->Form->input('Rideoffer.DepartureTime', array(
        'type' => 'time',
        'interval' => 5,
        'selected' => '09:00:00',
    ));
    ?>
    

    【讨论】:

    • “默认”不是更好的选择吗?
    • "selected" 通常不是最好的方法(它会在 POST 之后破坏表单),"default" 在大多数情况下是正确的(对于复选框不是)- 请阅读文档:book.cakephp.org/2.0/en/core-libraries/helpers/…跨度>
    【解决方案2】:

    设置默认数据的最佳方式是使用控制器(仅当未发布时):

    if (!$this->request->is('post')) {
        $this->request->data['Rideoffer']['DepartureTime'] = '09:00:00';
    }
    

    见http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#common-options

    这适用于所有表单元素。

    表单的“selected”、“value”、“checked”和其他硬编码属性通常会在发布不成功后破坏表单(如果表单包含验证错误):它会丢失所有输入的数据并将它们重置为之前的值,这对于前端用户来说通常很烦人。

    见http://www.dereuromark.de/2010/06/23/working-with-forms/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多