php – symfony2自定义时间选择字段

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – symfony2自定义时间选择字段脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试制作自定义日期时间表单字段.
就像在这answer中一样,我使用DataTransformer分割了具有一个日期表单字段和时间表单字段的日期时间字段.

我使用jquery datepicker进行日期选择,但我希望有一个自定义选择表单字段用于时间选择,小时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",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',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);

        $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;
}
}

表单显示是正确的,但是当我提交表单时,我收到此错误

而我将分割项目的日期时间自定义选项正确保存到DB.

我认为reverseTransform函数返回datetime格式,表单生成器无法将其转换为选择格式:

你能告诉我怎么能摆脱这个错误吗?

这样做有更好的方法吗?

解决方法

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);

脚本宝典总结

以上是脚本宝典为你收集整理的php – symfony2自定义时间选择字段全部内容,希望文章能够帮你解决php – symfony2自定义时间选择字段所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。