简体   繁体   English

为什么总是以带字段日期的形式为null? (在symfony2中)

[英]Why always null in form with field date? (in symfony2)

I have a problem with Form in Symfony2, 我在Symfony2中的表单有问题,

This is mi form Code: 这是mi形式的代码:

 $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. 字段“ fecha”不起作用,我测试了另一种格式,但是我没有解决方案,总是说“ null”,我尝试生成JavaScript以显示一个日历,但问题是相同的,始终为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. 今天,“ fecha”字段始终如此。

And the field 'fentrega' with the widget choise work perfect. 带有小部件选择的字段“ fentrega”非常完美。

Anyone can helpme please? 任何人都可以帮我吗?

Thanks in advance. 提前致谢。

Well, now work perfect, the problem is the format of the date. 好吧,现在可以正常工作了,问题在于日期的格式。

With this code work correct: 使用此代码可以正确运行:

the controller: 控制器:

$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'))
            ->add('fentrega', 'date',array('format' => 'yyyy-MM-dd'))
            ->add('estado','hidden', array('data' => '3'))
            ->add('Guardar', 'submit')
            ->getForm();

in the view: 在视图中:

$('#form_fecha').datepicker({
            dateFormat : 'd-m,-yy',
            prevText : '<i class="fa fa-chevron-left"></i>',
            nextText : '<i class="fa fa-chevron-right"></i>',

        });

the problem is 'format' => 'dM-y') is equal to dateFormat : 'd-mm-yy' the reason... i don't know... 问题是'format' => 'dM-y')等于dateFormat : 'd-mm-yy'原因...我不知道...

Thanks all for the help 谢谢大家的帮助

Do not use __construct() to set default data. 不要使用__construct()设置默认数据。 After object is created (by __construct) Doctrine do its magic and override values set by a constructor. 创建对象后(通过__construct),Doctrine会执行其魔术操作并覆盖由构造函数设置的值。 Prepare method setDefauts() and put setting default data there. 准备方法setDefauts()并将设置默认数据放在那里。 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();
}
// ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM