我正在尝试在我的表单中使用 'type'=> 'datetime-local' 来创建日期时间选择器。在视图中一切正常,但 cakephp 无法处理结果的格式。我试图将它转换为 Cake\I18n\Time 但它忽略了设定的时间并使用当前时间。
有人在 cakephp 中使用 html5 日期时间选择器吗?有没有打算让它成为蛋糕的默认选择器?
带有很多选择框的 cakephp 默认选择选项并不是很方便。
我正在尝试在我的表单中使用 'type'=> 'datetime-local' 来创建日期时间选择器。在视图中一切正常,但 cakephp 无法处理结果的格式。我试图将它转换为 Cake\I18n\Time 但它忽略了设定的时间并使用当前时间。
有人在 cakephp 中使用 html5 日期时间选择器吗?有没有打算让它成为蛋糕的默认选择器?
带有很多选择框的 cakephp 默认选择选项并不是很方便。
CakePHP 不支持YYYY-MM-DDTHH:MM
开箱即用的 HTML5 本地日期时间输入 ( ) 使用的格式(目前)。
您可以通过例如使用自定义/扩展数据库类型来解决此问题,该类型将适当的格式添加到现有默认格式(这将影响所有日期时间字段/列),例如:
// in src/Database/Type/DateTimeType.php
namespace App\Database\Type;
class DateTimeType extends \Cake\Database\Type\DateTimeType
{
public function __construct($name = null)
{
$this->_format[] = 'Y-m-d\TH:i'; // date() compatible format
parent::__construct($name);
}
}
// in config/boostrap.php before `Type::build('datetime')` is invoked
Type::map('datetime', \App\Database\Type\DateTimeType::class);
通过在编组之前手动解析输入:
// in a table class, a behavior, or a listener for the Model.beforeMarshal event
public function beforeMarshal(\Cake\Event\Event $event, \ArrayObject $data, \ArrayObject $options)
{
if (isset($data['fieldName']) &&
is_string($data['fieldName'])
) {
$data['fieldName'] = \Cake\I18n\Time::parseDateTime(
$data['fieldName'],
"yyyy-MM-dd'T'HH:mm" // ICU compatible format
);
}
}
或通过启用本地化解析并提供匹配格式(这会将所有日期时间字段/列限制为这种格式):
// in config/bootstrap.php
Type::build('datetime')
->useImmutable()
->useLocaleParser()
->setLocaleFormat("yyyy-MM-dd'T'HH:mm"); // ICU compatible format
也可以看看