1

I have this Symfony 4.3.11 API rest application :

Here is my post request body:

{
    "publicationDate": "2019-03-13"
}

Entity :

   /**
     * @var \DateTimeImmutable
     *
     * @ORM\Column(type="datetime_immutable")
     *
     * @Assert\NotBlank(message="required.field")
     * @Assert\Date(message="invalid.date")
     */
    private $publicationDate;

MyEntityType :

    $builder
       ->add('publicationDate', DateTimeType::class, [
            'input'           => 'datetime_immutable',
            'widget'          => 'single_text',
            'format'          => 'yyyy-MM-dd',
            'invalid_message' => 'invalid.date',
        ]);

The Post Submit data looks like :

object(App\Entity\MyEntity)[3996]
  private 'id' => null
  private 'publicationDate' => 
    object(DateTimeImmutable)[5718]
      public 'date' => string '2019-03-13 00:00:00.000000' (length=26)
      public 'timezone_type' => int 3
      public 'timezone' => string 'Europe/Paris' (length=12)

And this works fine.

But now, I've a basic Symfony 5.0.5 app with the same Entity. The form type is slightly different (the html5 option should be disabled) :

MyEntityType :

    $builder
       ->add('publicationDate', DateTimeType::class, [
            'input'           => 'datetime_immutable',
            'html5'           => false,
            'widget'          => 'single_text',
            'format'          => 'yyyy-MM-dd',
            'invalid_message' => 'invalid.date',
        ]);

The Post Submit data is exactly the same as above.

But when submitting, I've a validation error :

array (size=1)
  'publicationDate' => 
    array (size=1)
      0 => string 'This value should be of type string.'

Has anything changed in datetimetype between S4 and S5 ?

yivi
  • 42,438
  • 18
  • 116
  • 138
Chuck Norris
  • 1,125
  • 1
  • 12
  • 28
  • 1
    Check the answer there. Basically, you need to use `Assert\Type`. – yivi Mar 12 '20 at 15:28
  • Yeah, that's exactly my problem. Sorry I've searched it before but I guess I haven't the right keywords :) – Chuck Norris Mar 12 '20 at 15:38
  • 1
    That's fine. Since the question is closed as a duplicate, it will help other users using the same terminology than you to get to the correct answer. – yivi Mar 12 '20 at 15:39
  • I think it come from your getter/setter. check them declared accord to php7 `getPublicationDate(): ?DateTimeImmutable` and `setPublicationDate(?DateTimeImmutable $publicationDate)` – Theva Mar 12 '20 at 16:26

0 Answers0