2013-01-13 77 views
4

我想制作自定义日期时间表单字段。 像这样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',...)

你能告诉我怎样才能摆脱这种错误的?

有没有更好的方法来做到这一点?

+0

可能是你的实体或你存储日期的数据库被定义为一个INT。你为什么不把它们复制到你的问题中? – Xocoatzin

+0

我已经更新了问题,实体的对象是日期时间对象 – Alex

回答

2

由datatransformers转换函数返回的数据数组是由DateTime对象组成的数组。这些对象不能转换为选择字段值。为了解决这个问题返回实际格式化的日期和时间字符串:

$result = array(
    'date' => $date->format('Y-m-d'), 
    'time' => $time->format('H:i') 
); 

另外,reverseTransform功能应该然后将这些字符串转换回一个实际的DateTime对象,使用下面的代码:

$dateTimeString = $array['date'] . ' ' . $array['time']; 
return DateTime::createFromFormat('Y-m-d H:i', $dateTimeString); 
+0

感谢您的回答,但它仍然不起作用: 如果我在转换函数 '$ result = array( 'date'=> $ date - >格式( 'YM-d'), '时间'=> $时间>格式( 'H:I') );' 此错误出现:致命错误:调用一个成员函数格式()上非对象 – Alex

+0

如果我使用此在变换: '$结果=阵列( '日期'=> $日期, '时间'=> $时间 );' 而这在reverseTRansform: '$ dateTimeString = $ array ['date']。 ''。 $阵列[ '时间']; 返回日期时间:: createFromFormat( 'Y-M-d H:I',$ dateTimeString);' 此错误出现:捕致命错误:类日期时间的对象不能被转换成字符串 – Alex