简体   繁体   English

有没有办法在CakePHP日期表单输入中设置默认日期?

[英]Is there a way to set the default date in a CakePHP date form input?

I have this: 我有这个:

<?php echo $this->Form->input('Schedule.0.end_date', array(
    'minYear' => date('Y'),
    'maxYear' => date('Y')+5
)); ?>

I would like to set the default date to something other than today. 我想将默认日期设置为今天以外的其他日期。 Is this possible with CakePHP's form helper? CakePHP的表单助手是否可以实现这一点?

I found a post that showed how do to it with TIME - but trying something similar by setting "day", "month", "year" does nothing. 我找到了一篇文章,展示了如何使用TIME - 但通过设置“day”,“month”,“year”尝试类似的东西什么也没做。

You can achieve that using the selected parameter of $this->Form->input(); 您可以使用$this->Form->input();selected参数来实现这一点$this->Form->input(); . Try like this: 试试这样:

<?php
echo $this->Form->input('datetime', array(
  'label' => 'Date 1',
  'selected' => array(
    'day' => '',
    'month' => '',
    'year' => '',
    'hour' => '',
    'minute' => '',
    'second' => ''
    )
  ));
/* What's interesting... this will work aswell: */
echo $this->Form->input('datetime', array(
  'label' => 'Date 2',
  'selected' => '0000-00-00 00:00:00'
  ));
?>

Just an update for Cake 3.* users: now in order to precompile the datetime fields is necessary to use the 'default' keyword: 只是Cake 3 *用户的更新:现在为了预编译datetime字段,必须使用'default'关键字:

echo $this->Form->input('datetime', array(
  'label' => 'Date 2',
  'default' => '2015-09-10 06:40:00'
));

The problem with using the 'selected' option in the form helper, is if you submit the form, then the submitted value will be overwritten by the selected value, so if the page reloads because of an error or something, then the user loses what was original submitted. 在表单助手中使用'selected'选项的问题是,如果你提交表单,那么提交的值将被选中的值覆盖,所以如果页面由于错误或其他东西而重新加载,那么用户会失去什么是原始提交。

I prefer to set the default value in the controller if it hasn't already been set, otherwise it will be populated by the last submission. 我更喜欢在控制器中设置默认值(如果尚未设置),否则将由最后一次提交填充。

In Controller: 在控制器中:

if (!isset($this->request->data['start_date']))
        $this->request->data['start_date'] = date('Y-m-d', strtotime('-1 month'));

This way the first time the user loads the form, they are presented with a desirable default value, but if they submit the form once and are brought back to it, their selected value is selected still. 这种方式在用户第一次加载表单时,会向其显示所需的默认值,但如果他们提交表单一次并将其返回给它,则仍会选择其选定的值。

With CakePHP 2.x, 使用CakePHP 2.x,

echo $this->Form->input('end', array(
     'selected' => array(
         'day' => date('d'), 
         'month' => date('m'), 
         'year' => date('Y'), 
         'hour' => date('h'), 
         'min' => date('i'), 
         'meridian' => date('a')
)));

if you want to show month number in English words: 如果你想用英文单词显示月号:

$month = DateTime::createFromFormat('!m', date('m'));
$month->format('F');

echo $this->Form->input('end', array(
     'selected' => array(
         'day' => date('d'), 
         'month' => $month->format('F'), 
         'year' => date('Y'), 
         'hour' => date('h'), 
         'min' => date('i'), 
         'meridian' => date('a')
)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM