Symfony2形成datetime和datetimepicker

时间:2016-05-18 08:44:09

标签: php forms symfony datetime datetimepicker

我目前正在尝试使用datetimepicker添加表单字段,因为我提交即时收到以下错误

This value is not valid.    pesagem1    
Symfony\Component\Validator\ConstraintViolation
Object(Symfony\Component\Form\Form).children[pesagem1] = 27/04/2016 23:59
Caused by:
Symfony\Component\Form\Exception\TransformationFailedException
Unable to reverse value for property path "pesagem1": DateTime::__construct(): Failed to parse time string (27/04/2016 23:59) at position 0 (2): Unexpected character
Caused by:
Symfony\Component\Form\Exception\TransformationFailedException
DateTime::__construct(): Failed to parse time string (27/04/2016 23:59) at position 0 (2): Unexpected character
Caused by:
Exception
DateTime::__construct(): Failed to parse time string (27/04/2016 23:59) at position 0 (2): Unexpected character

这是我的实体+表格

//实体

/** @ORM\Column(type="datetime") */
private $pesagem1;

// FORM

        ->add('pesagem1','datetime', array(
                'widget' => "single_text",
                'html5' => false,
                'date_format'=>"dd/MM/yyyy hh:mm",
                'attr'=>array(
                    'class'=>'datetimepickz form-control',
                    'style' => 'width: 70%',
                    'data-format'=>"dd/MM/yyyy hh:mm"
                )
            )
        )

js + twig

$('.datetimepickz').datetimepicker({

        locale: moment.locale('pt')

    });


<div class="form-group">

                        <label>Primeira Pesagem</label>

                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-envira"></i></span>
                            {{ form_widget(form.pesagem1) }}
                        </div>
                        <div>
                            {{ form_errors(form.pesagem1) }}
                        </div>
                    </div>

像字符串这样的接缝是&#34;正确&#34;根据symfony2 profiler,已经尝试过日期格式dd / mm / yyy hh:mm,d / m / Y hh:mm。并且所有人都返回同样的问题。

非常感谢任何帮助或提示。 TY

1 个答案:

答案 0 :(得分:0)

阅读Should I use field 'datetime' or 'timestamp'?

我改变了日期格式,现在一切都接近了。

$('.datetimepickz').datetimepicker({
        locale: moment.locale('pt'),
        format: 'YYYY-MM-DD HH:mm'
    });