2013-03-11 39 views
0

我希望在ctp時間下拉列表中顯示默認時間爲上午9:00。以下是我的ctp代碼:在cakephp中顯示默認時間ctp

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

                 'interval' => 5 
                 )); 
          ?> 

我該怎麼做?

回答

1

使用 '選擇' 選項

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

是不是「默認」更好的選擇? – mark 2013-03-11 11:06:34

+0

不,不適用於SELECT元素 – kaklon 2013-03-11 11:09:05

+0

「selected」通常不是最好的方法(它在POST後破壞表單),在大多數情況下,「default」是正確的(對於複選框不是) - 請閱讀文檔:http: //book.cakephp.org/2.0/en/core-libraries/helpers/form.html#common-options – mark 2013-03-11 11:10:58

0

設置默認的數據使用控制器的最佳方式(且僅當不貼):

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/