drkjzk3359 2014-08-20 19:28
浏览 40

为什么总是在带有字段日期的表单中为空 (在symfony2中)

I have a problem with Form in Symfony2,

This is mi form Code:

 $form = $this->createFormBuilder($pedido)
            ->setAction($this->generateUrl("ec_main_guardar_pedido"))
            ->add('cliente','entity',array('class' =>'ECMainBundle:Clientes','property'=>'cliente'))
            ->add('fecha', 'date', array('widget' => 'single_text',
                                         'format' => 'd-M-y',
                                         'required' => 'false',
                                         'read_only' => 'false'))
            ->add('fentrega', 'date',array('format' => 'yyyy-MM-dd'))
            ->add('estado','hidden', array('data' => '3'))
            ->add('Guardar', 'submit')
            ->getForm();

The field 'fecha' don't work, i test with another formats, but i dont have solution, always say 'null', i try to generate the javascript to show one calendar, but the problem is the same, always null.

If in the Entity add this:

public function __construct()
    {
        $this->lineas = new ArrayCollection();
        $this->fecha= new \DateTime();
        $this->fentrega= new \DateTime();
    }

the field 'fecha' always is today.

And the field 'fentrega' with the widget choise work perfect.

Anyone can helpme please?

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • duancaishi1897 2014-08-20 20:01
    关注

    Do not use __construct() to set default data. After object is created (by __construct) Doctrine do its magic and override values set by a constructor. Prepare method setDefauts() and put setting default data there. Then, after created model, ie. in controllers action run this method.

    // /Acme/FooBundle/Entity/Bar.php
    // ...
    public function setDefaults()
    {
        $this->fecha = new \DateTime();
        $this->fentrega = new \DateTime();
    }
    // ...
    
    
    // /Acme/FooBundle/Controller/BarController.php
    // ...
    public function someAction()
    {
        $var = new Bar();
        $var->setDefaults();
    }
    // ...
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)