如何为 Typo3 6.2 Extbase 扩展设置可能的日期输入格式?
How to set possible date input formats for Typo3 6.2 Extbase Extension?
我使用 Fluid 6.2 和 Extbase 6.2 在 Typo3 6.2 中使用 Extension Builder 进行了扩展。
我用日期 属性.
创建了约会对象
我想以“dd.mm.yyyy”格式输入日期。
所以我尝试了这个:
它给出了这个错误:
我很无能,因为我对此不熟悉,我想以一种好的方式解决这个问题。
我的 createAction
代码只是由扩展构建器生成的,因此看起来像这样:
/**
* action create
*
* @param \JH\Appmgmt\Domain\Model\Appointment $newAppointment
* @return void
*/
public function createAction(\JH\Appmgmt\Domain\Model\Appointment $newAppointment) {
$this->addFlashMessage('The object was created. Please be aware that this action is publicly accessible unless you implement an access check. See <a href="http://wiki.typo3.org/T3Doc/Extension_Builder/Using_the_Extension_Builder#1._Model_the_domain" target="_blank">Wiki</a>', '', \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR);
$this->appointmentRepository->add($newAppointment);
$this->redirect('list');
}
现在我意识到,如果我在此处更改某些内容以使格式正常工作,我将不得不在 updateAction
以及我还不知道的其他内容中执行相同的操作。
我也拼命尝试以某种方式将其部分格式化为所需的格式,但这注定会失败 - 结果相同:
<f:form.textfield property="appointmentDate" value="{appointment.appointmentDate->f:format.date(format:'Y-m-d\TH:i:sP')}" /><br />
这就是我需要你帮助的地方 - 我不知道在哪里以及如何在全球范围内允许这种日期格式,因为我在其他领域也需要它。
我唯一能想到的是改变领域模型中的一些东西:
/**
* appointmentDate
*
* @var \DateTime
*/
protected $appointmentDate = NULL;
但我不知道该如何处理。 :(
有人有想法吗?
您发送的日期格式不正确作为日期对象。
这正是错误所说的。
您可以做的是重新格式化您发送的日期,以便它作为您的对象的有效参数到达您的控制器操作。这是通过调用 属性 映射的 initialize
操作完成的。
可以在此处找到一个清晰的示例:
http://www.kalpatech.in/blog/detail/article/typo3-extbase-datetime-property-converter.html
您需要的部分是:
// Here we convert the property mapping using the property mapper
public function initializeAction() {
if ($this->arguments->hasArgument('newCalendar')) {
$this->arguments->getArgument('newCalendar')->getPropertyMappingConfiguration()->forProperty('startdate')->setTypeConverterOption('TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter',\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT,'d-m-Y');
}
}
您还可以禁用对控制器的日期参数的验证,并从您的 'date' 创建一个有效的日期对象,然后使用 setAppointmentDate($yourNewDateObject)
。
然后你绕过 extbase 验证,这不是一个好的做法。
我使用 Fluid 6.2 和 Extbase 6.2 在 Typo3 6.2 中使用 Extension Builder 进行了扩展。
我用日期 属性.
创建了约会对象
我想以“dd.mm.yyyy”格式输入日期。
所以我尝试了这个:
它给出了这个错误:
我很无能,因为我对此不熟悉,我想以一种好的方式解决这个问题。
我的 createAction
代码只是由扩展构建器生成的,因此看起来像这样:
/**
* action create
*
* @param \JH\Appmgmt\Domain\Model\Appointment $newAppointment
* @return void
*/
public function createAction(\JH\Appmgmt\Domain\Model\Appointment $newAppointment) {
$this->addFlashMessage('The object was created. Please be aware that this action is publicly accessible unless you implement an access check. See <a href="http://wiki.typo3.org/T3Doc/Extension_Builder/Using_the_Extension_Builder#1._Model_the_domain" target="_blank">Wiki</a>', '', \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR);
$this->appointmentRepository->add($newAppointment);
$this->redirect('list');
}
现在我意识到,如果我在此处更改某些内容以使格式正常工作,我将不得不在 updateAction
以及我还不知道的其他内容中执行相同的操作。
我也拼命尝试以某种方式将其部分格式化为所需的格式,但这注定会失败 - 结果相同:
<f:form.textfield property="appointmentDate" value="{appointment.appointmentDate->f:format.date(format:'Y-m-d\TH:i:sP')}" /><br />
这就是我需要你帮助的地方 - 我不知道在哪里以及如何在全球范围内允许这种日期格式,因为我在其他领域也需要它。
我唯一能想到的是改变领域模型中的一些东西:
/**
* appointmentDate
*
* @var \DateTime
*/
protected $appointmentDate = NULL;
但我不知道该如何处理。 :( 有人有想法吗?
您发送的日期格式不正确作为日期对象。 这正是错误所说的。
您可以做的是重新格式化您发送的日期,以便它作为您的对象的有效参数到达您的控制器操作。这是通过调用 属性 映射的 initialize
操作完成的。
可以在此处找到一个清晰的示例: http://www.kalpatech.in/blog/detail/article/typo3-extbase-datetime-property-converter.html
您需要的部分是:
// Here we convert the property mapping using the property mapper
public function initializeAction() {
if ($this->arguments->hasArgument('newCalendar')) {
$this->arguments->getArgument('newCalendar')->getPropertyMappingConfiguration()->forProperty('startdate')->setTypeConverterOption('TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter',\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT,'d-m-Y');
}
}
您还可以禁用对控制器的日期参数的验证,并从您的 'date' 创建一个有效的日期对象,然后使用 setAppointmentDate($yourNewDateObject)
。
然后你绕过 extbase 验证,这不是一个好的做法。