Forms DateTime对象在form POST之后转换为字符串类型

Forms DateTime对象在form POST之后转换为字符串类型,forms,symfony,datetime,post,doctrine-orm,Forms,Symfony,Datetime,Post,Doctrine Orm,我有一个表单可以编辑新闻实体。这是: <form action="{{path('validate_news', {'id': news.id})}}" method="POST" > <div class="form-group row"> <label for="Text" class="col-sm-4 col-form-label">Text</label> <div class="col-sm-5"> &l

我有一个表单可以编辑
新闻
实体。这是:

<form action="{{path('validate_news', {'id': news.id})}}" method="POST" >
  <div class="form-group row">
  <label for="Text" class="col-sm-4 col-form-label">Text</label>
  <div class="col-sm-5">
    <textarea name = "text" type="text" class="form-control" id="text" aria-describedby="textHelp" placeholder="{{news.text|e}}"></textarea>
    </div>
  </div>
  <div class="form-group row">
    <label for="startDate" class="col-sm-4 col-form-label">Start Date</label>
    <div class="col-sm-5">
    <input name = "startDate" type="date" class="form-control" id="startDate" aria-describedby="startDate" placeholder="{{news.startDate|date('d-m-Y')}}">
    </div>
  </div>
  <div class="form-group row">
   <label for="expireDate" class="col-sm-4 col-form-label">Expire Date</label>
   <div class="col-sm-5">
    <input name = "expireDate" type="date" class="form-control" id="expireDate" aria-describedby="emailHelp" placeholder="{{news.expireDate|date('d-m-Y')}}">
    </div>
   </div>

  <button type="submit" class="btn btn-primary">Submit</button>
</form>
startDate
expireDate
属性定义为
datetime

当我尝试刷新
News
对象时,出现以下错误:

FatalErrorException in DateTimeType.php line 53:
Error: Call to a member function format() on a non-object
在控制器中的
$startDate
上启动
var\u dump
,我可以看到它有一个
字符串
类型。但在形式上,我将其定义为
datetime


为什么这会发生在
POST
调用上?

因为您从表单中获取字符串值。您需要为实体设置DateTime对象

试试看

$startDate = new \DateTime($request->get('startDate'));

因为您正在从表单中获取字符串值。您需要为实体设置DateTime对象

试试看

$startDate = new \DateTime($request->get('startDate'));

因为您正在从表单中获取字符串值。您需要为实体设置DateTime对象。尝试这样做
$startDate=new\DateTime($request->get('startDate')@MertÖksüz谢谢,成功了!在我的示例中,我首先创建了一个DateTime对象,然后给它形式值。您的方法是正确的。我的回答是:)因为您正在从表单中获取字符串值。您需要为实体设置DateTime对象。尝试这样做
$startDate=new\DateTime($request->get('startDate')@MertÖksüz谢谢,成功了!在我的示例中,我首先创建了一个DateTime对象,然后给它形式值。你的方法是正确的。我的回答是:)