html - cakephp 3.6 中的 'type'=> 'datetime-local'
问题描述
我正在尝试在我的表单中使用 '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
也可以看看
推荐阅读
- powershell - 如何在powershell中撤销certificatevalidationcallback
- sql - 使用部分索引排除约束 - Postgresql
- node.js - 使用 : 在 json 键中解析 nodejs 中的 json
- node.js - how to access field name in mongoose $aggregate->$lookup-> pipeline->$match
- c# - 带有标题和正文的 SendAsync HttpPost 的困难
- c# - 签名 xml 中数字签名中的时间戳
- javascript - EJS / Mongoose - 帖子在 POST 之后往往会显示两次,当我尝试获取同一页面时,它会显示两次?
- java - URI 未正确编码
- excel - 在列中查找特定单词并将相邻的值复制到不同的列
- apache-spark - 从本地提交 Spark 作业到 emr ssh 设置