livioribeiro / nette-datetimefields
用于日期、时间和日期时间输入的表单组件,自动转换为 DateTime。
dev-master
2014-04-07 20:14 UTC
Requires
- php: >= 5.3.0
- nette/nette: >= 2.0.0
This package is not auto-updated.
Last update: 2024-09-28 15:28:52 UTC
README
用于日期、时间和日期时间输入的表单组件,自动转换为 DateTime。
设置
将 DateTimeFormExtension 添加到您的 config.neon 配置文件中
extensions: - NetteDateTimeFields\DateTimeFormExtension
或者在 bootstrap.php 中调用 register
方法
use NetteDateTimeFields\DateTimeFormExtension DateTimeFormExtension::register();
使用方法
简单地在 Form 对象上调用 addDate()
、addTime()
或 addDateTime()
$form = new Nette\Application\UI\Form(); $form->addDate('date', 'date', $format = 'd/m/Y'); $form->addTime('time', 'time', $format = 'H:i'); $form->addDateTime('dateTime', 'dateTime', $dateFormat = 'd/m/Y', $timeFormat = 'H:i', $separator = ' ')
您还可以添加范围验证
use NetteDateTimeFields\Controls\DateTimeBase; $form = new Nette\Application\UI\Form(); $form->addDate('date', 'date', 'Y-m-d') ->addRule(DateTimeBase::DATETIME_RANGE, 'message', ['1970-01-01', '2070-01-01']); // \DateTime or string $form->addTime('time', 'time') ->addRule(DateTimeBase::DATETIME_RANGE, 'message', ['08:00', '16:30']); $form->addDateTime('dateTime', 'dateTime') ->addRule(DateTimeBase::DATETIME_RANGE, 'message', ['01/01/1970', '01/01/2070']);
参数可以是 \DateTime 对象或按照指定格式格式化的字符串。
日期时间范围验证可以接收 4 个参数,其中第三个和第四个参数验证给定日期的时间范围。例如,以下将不会验证日期和时间 '01/01/2010 07:00'。
$form->addDateTime('dateTime', 'dateTime') ->addRule(DateTimeBase::DATETIME_RANGE, 'message', ['01/01/1970', '01/01/2070', '08:00', '16:30']);