【问题标题】:How do I limit the dates a user can choose in CakePHP FormHelper's date field?如何限制用户可以在 CakePHP FormHelper 的日期字段中选择的日期?
【发布时间】:2023-03-13 18:36:01
【问题描述】:

我想将用户选择的日期限制在星期一。

我将日期字段存储在 MySQL 数据库中,当我使用 echo $this->Form->input('date'); 时,CakePHP 会在添加/编辑表单上自动为我生成一个日期选择器。但它会生成年、月和日的下拉菜单。这似乎不是将用户选择限制为每月四五天的好方法。

我查看了 CakePHP 网站上的 options available for the datetime input fieldinterval 正是我需要的,但它只适用于日期时间字段上的分钟框,并且似乎对日期没有影响字段。

有没有一种干净的 CakePHP 方法可以解决这个问题,还是我自己生成日期列表并使用这些值创建一个下拉框?

谢谢!

【问题讨论】:

  • 你为什么不用jquery datepicker..你也可以在datepicker上限制日期
  • 这是一个不错的选择。我想没有干净的方法可以让 Cake 做到这一点?
  • 你也许可以。但这会花费你很多时间。实现你所期望的改变非常少 - 我认为最好的选择是 datepicker :)

标签: php date cakephp formhelper


【解决方案1】:

如果你想有一个漂亮的 UI 使用 Jquery DatePicker

//Cake view
    echo $this->Form->input('created', array('type'=>'text', 'id'=>'created'));

//Jquery 
    $('#created').datepicker({
         dateFormat : 'yy-mm-dd'
    });   

否则可以限制日期输入

//Year
echo $this->Form->year('purchased-year', 2000, date('Y'));
//Month
echo $this->Form->month('mob');
//Day
echo $this->Form->day('created');

FormHelper::hour(string $fieldName, boolean $format24Hours, array $attributes)
Creates a select element populated with the hours of the day.

FormHelper::minute(string $fieldName, array $attributes)
Creates a select element populated with the minutes of the hour.

FormHelper::meridian(string $fieldName, array $attributes)
Creates a select element populated with ‘am’ and ‘pm’.

http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#creating-date-and-time-inputs

【讨论】:

  • 谢谢,成功了!绝对是我见过的最干净的解决方案。
猜你喜欢
  • 1970-01-01
  • 2014-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-09
  • 1970-01-01
相关资源
最近更新 更多