8

When i submit my form it shows me this error:

An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class DateTime could not be converted to string").

Full message Error

I don't know what i'm doing wrong , this is my code ( PHP )

   ->add('purchasedate', DateType::class, array(
            'widget' => 'single_text',
            'html5' => false,
            'label' => 'Date d’achat (JJ/MM/AAAA)*',
          ))

Purchase Date:

/**
 * @var string
 *
 * @ORM\Column(name="purchasedate", type="date")
 * @Assert\Range(
 *      min = "2019-01-14",
 *      max = "2019-04-14"
 * )
 * 
 * 
 * @Serializer\SerializedName("purchasedate")
 * @Serializer\Expose()
 * 
 */

private $purchasedate;

Get and Set Method

Solution

I was trying to display the date in a twig template is where the error showed up. I only update the display code

{{ user.purchasedate|date('d-m-Y') }}
8
  • What is the type of purchasedate property of underlying entity object? Full exception message would be helpful too. Commented Jan 8, 2019 at 10:58
  • 1
    I updated the question Commented Jan 8, 2019 at 11:02
  • can you show your get and set method. Commented Jan 8, 2019 at 11:19
  • Check the question , i updated Commented Jan 8, 2019 at 11:21
  • change annotations of getters and setters to date instead of string Commented Jan 8, 2019 at 11:26

2 Answers 2

19

just add twig filter to your date

{{ mydate |date('d-m-Y') }}
Sign up to request clarification or add additional context in comments.

Comments

2
->add('purchasedate', DateType::class, array(
    'widget' => 'single_text',
    'html5' => false,
    'label' => 'Date d’achat (JJ/MM/AAAA)*',
    'format' => 'yyyy-MM-dd',
));

if not

$date = DateTime::createFromFormat(format, db_date);

$date = $date->format('d/m/Y'); 

1 Comment

I'm worried about something else :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.