我想制作自定义日期时间表单字段。 像这样answer我使用DataTransformer将日期时间字段与一个日期表单字段和时间表单字段分开。symfony2自定义时间选择字段
我使用的日期选择一个jQuery日期选择器,但我想有时间选择ONE自定义选择表单字段蒙山半小时intervalls:
00:00
00:30
...
23:30
这里是我的代码,但我不知道如何接近这个
在我的实体
/**
* Time
*
* @ORM\Table(name="time")
* @ORM\Entity
*/
class Time
{
...
/**
* @var \DateTime
*
* @ORM\Column(name="begin_date", type="datetime", nullable=false)
*/
private $beginDate;
/**
* @var \DateTime
*
* @ORM\Column(name="end_date", type="datetime", nullable=false)
*/
private $endDate;
...
/**
* Set beginDate
*
* @param \DateTime $beginDate
* @return Time
*/
public function setBeginDate($beginDate)
{
$this->beginDate = $beginDate;
return $this;
}
/**
* Get beginDate
*
* @return \DateTime
*/
public function getBeginDate()
{
$this->beginDate;
}
/**
* Set endDate
*
* @param \DateTime $endDate
* @return Time
*/
public function setEndDate($endDate)
{
$this->endDate = $endDate;
return $this;
}
/**
* Get endDate
*
* @return \DateTime
*/
public function getEndDate()
{
return $this->endDate;
}
...
}
表单类型
class TimeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('beginDate', 'my_datetime', array('label' => 'label.form.date'))
->add('endDate', 'my_datetime', array('label' => 'label.form.date'));
}
public function getName()
{
return 'Time';
}
}
因此,这里是我的自定义表单类型:
class MyDateTimeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('date', 'genemu_jquerydate', array('input' => 'datetime','widget' => 'single_text','format' => 'dd/MM/yyyy','error_bubbling' => true))
->add('time', 'choice', array(
'choices' => array(
'00:00' => '00:00',
'00:30' => '00:30',
'01:00' => '01:00',
...
'22:30' => '22:30',
'23:00' => '23:00',
'23:30' => '23:30',
),'error_bubbling' => true
));
$builder->appendClientTransformer(new DateTimeToDateTimeArrayTransformer());
}
public function getDefaultOptions(array $options)
{
return array(
'label' => 'label.form.date',
'error_bubbling' => false
);
}
public function getName()
{
return 'my_datetime';
}
}
这是DataTransformer:
class DateTimeToDateTimeArrayTransformer implements DataTransformerInterface
{
public function transform($datetime)
{
if(null !== $datetime)
{
$date = clone $datetime;
$date->setTime(12, 0, 0);
$time = clone $datetime;
$time->setDate(1970, 1, 1);
}
else
{
$date = null;
$time = null;
}
$result = array(
'date' => $date,
'time' => $time
);
return $result;
}
public function reverseTransform($array)
{
$date = $array['date'];
// $time = $array['time']; // Fatal error: Call to a member function format() on a non-object
$time = new \DateTime($array['time']);
if(null == $date || null == $time)
return null;
$date->setTime($time->format('G'), $time->format('i'));
return $date;
}
}
表显示是正确的,但是当我提交表单我得到这个错误:
Notice: Object of class DateTime could not be converted to int in /var/www/ttime/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php line 457 500 Internal Server Error - ErrorException
虽然我已将分割项目datetime +自定义选项正确地持久保存到数据库。
我认为reverseTransform函数的返回日期时间格式,以及表单生成器无法将其转换为选择格式:
array('00:00' => '00:00',...)
你能告诉我怎样才能摆脱这种错误的?
有没有更好的方法来做到这一点?
可能是你的实体或你存储日期的数据库被定义为一个INT。你为什么不把它们复制到你的问题中? – Xocoatzin
我已经更新了问题,实体的对象是日期时间对象 – Alex