0

In the entity the date time is decalared:

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

in the form

$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

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

goto
  • 7,908
  • 10
  • 48
  • 58
Wassim Jied
  • 25
  • 1
  • 5
  • What is `...`, your entity ?Can you edit your post and add your entity setter? Note than you should use the `{}` symbol in the editor to format your code properly (I did it for this post ;)). – goto Feb 16 '18 at 11:59
  • 2
    Most probably you have method `setTaskDateStart(\DateTime $taskDateStart)` which does not allow passing `null` to it. – malarzm Feb 16 '18 at 12:04

2 Answers2

2

The problem was solved by modifying the setter (new setter without prototype):

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

become

 function setTaskDateStart( $taskDateStart ) {
     $this->taskDateStart = $taskDateStart;
 }
Wassim Jied
  • 25
  • 1
  • 5
2

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;

and

 function setTaskDateStart(?\DateTime $taskDateStart) {
     $this->taskDateStart = $taskDateStart;
 }
pevac
  • 591
  • 4
  • 8