user1645161
user1645161

Reputation: 15

Symfony / Form Type - not populating data for edit form for datetime in single_text widget style

Maybe something simple I missed? But I have a CRUD form based on an entity and everything seems to work fine. The object can be created, edited and updated fine. But when I change the default widget style on a datetime field the date data no longer populates into the edit form, and rather displays the date format string 'dd/mm/yyyy'.

Is there something obvious going wrong here?

In the entity the property is this:

/**
 * @ORM\Column(type="date", nullable=true)
 */
private $createdDate;

Getters and setters:

public function getCreatedDate(): ?\DateTimeInterface
{
    return $this->createdDate;
}

public function setCreatedDate(?\DateTimeInterface $createdDate): self
{
    $this->createdDate = $createdDate;

    return $this;
}

In the form builder:

->add('createdDate', DateType::class, [
            'widget' => 'single_text',
            'required' => false,
        ])

Generated HTML:

<div>
    <label for="hmr_core_createdDate" class="required">Created date</label>
    <input type="date" id="hmr_core_createdDate" 
        name="hmr_core[createdDate]" 
        required="required">
</div>

CreatedDate in the Request Attributes:

-createdDate: DateTime @1551092357 {#975 ▼
date: 2019-02-25 21:29:17.0 Australia/Melbourne (+11:00)

Debug output:

debug1 debug2

It doesn't seem to matter if I add options like format etc. As soon as I delete the widget line or set it to choice it all works.

Any ideas?

Upvotes: 0

Views: 699

Answers (1)

user1645161
user1645161

Reputation: 15

Ok, I fixed it. Somehow the entity object had been deleted from being passed as a parameter from the controller, I am not sure how it was recieving data in the case of the three drop down widgets but there it is.

Upvotes: 0

Related Questions