symfony 3.4 类型错误:参数 1 传递给 ... 必须是 DateTime 的实例,给定 null, [英] symfony 3.4 Type error: Argument 1 passed to ... must be an instance of DateTime, null given,

查看:64
本文介绍了symfony 3.4 类型错误:参数 1 传递给 ... 必须是 DateTime 的实例,给定 null,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实体中,日期时间被声明:

In the entity the date time is decalared:

 /**
 * @var \DateTime
 * @ORM\Column(name="task_date_start", type="datetime", nullable=true)
 */
 private $taskDateStart;

在表格中

$builder->add('taskDateStart', DateType::class, [
    "widget" => "single_text",
    "html5" => false,
    "format" => "dd-MM-yyyy"  ,
    "attr" => ["class" => "js-datepicker"], 
    "required" => false                                                    
]);

在字段中插入空值时出现此错误

i've this error when inserting null in to the field

传递给 ... 的参数 1 必须是 DateTime 的实例,给定 null

Argument 1 passed to ... must be an instance of DateTime, null given

推荐答案

更好的解决方案是允许设置 null 并让验证报告错误.

Better solution is to allow set the null and let the validation reports the error.

/**
 * @var \DateTime
 * @ORM\Column(name="task_date_start", type="datetime", nullable=false)
 * @Assert\NotBlank()
 */
 private $taskDateStart;

 function setTaskDateStart(?\DateTime $taskDateStart) {
     $this->taskDateStart = $taskDateStart;
 }

这篇关于symfony 3.4 类型错误:参数 1 传递给 ... 必须是 DateTime 的实例,给定 null,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆