1

I migrated a project from Symfony 3.4 to Symfony 4.4 (back side with API platform)

In an entity I have a date field, which is automatically generated on today's date, So when I make a POST or a PUT, she is not in my JSON

/**
     * @Groups({"get_logement", "post_logement", "put_logement"})
     * @ORM\Column(type="datetime", nullable=false)
     * @Assert\Date(message="the date is not correct.")
     */
    private $dateEtat;
public function getDateEtat(): ?\DateTimeInterface
    {
        return $this->dateEtat;
    }

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

        return $this;
    }

and a construct :

public function __construct()
    {
        $this->dateEtat = new \Datetime();
    }

since version 4.4 when I do a POST or a PUT, I get this error

"type": "https://tools.ietf.org/html/rfc2616#section-10",
  "title": "An error occurred",
  "detail": "dateEtat: Cette valeur doit être de type string.",
  "violations": [
    {
      "propertyPath": "dateEtat",
      "message": "This value must be of type string."
    }
  ]
yivi
  • 42,438
  • 18
  • 116
  • 138
jimwest
  • 11
  • 2

1 Answers1

4

See : https://github.com/symfony/symfony/issues/29646

Validating a \DateTimeInterface with "Symfony\Component\Validator\Constraints\DateTime" is deprecated since version 4.2.

Use "Symfony\Component\Validator\Constraints\Type" instead or remove the constraint if the underlying model is already type hinted to \DateTimeInterface.

Gaylord.P
  • 1,539
  • 2
  • 24
  • 54