-2

Me again :( I am really sorry ... Probably working to long and I can't see the solution.

I have a contact form. The form data is handled in the ContactController

/**
     * @Route("/contact", name="contact")
     * @param Request $request
     * @param EntityManagerInterface $em
     * @param MailerInterface $mailer
     * @return Response
     * @throws TransportExceptionInterface
     */
    public function index(Request $request, EntityManagerInterface $em, MailerInterface $mailer): Response
    {
        $contact = new Contact();
        $contactForm = $this->createForm(ContactType::class, $contact);
        $contactForm->handleRequest($request);
        if($contactForm->isSubmitted() && $contactForm->isValid()){
            //setting current date and time
            $date= new DateTime('now');
            $date->setTimezone(new \DateTimeZone(('Europe/Berlin')));
            $contact->setTimeAdded($date);
            
            //preparing E-Mail and sending it
            $email = (new TemplatedEmail())
                ->from('contact@menu.local')
                ->to('contact@menu.local')
                ->subject('Nachricht über das Kontaktformular')
                ->htmlTemplate('contact/mail.html.twig')
                ->context(['contact' => $contact]);
            $mailer->send($email);
            
            //saving contact data to database
            $em->persist($contact);
            $em->flush();
            
            //adding flash message
            $this->addFlash('success', 'Ihre Nachricht wurde versandt');
            
            //redirect to confirmation page
            return $this->render('contact/sentconf.html.twig',[
            'contact' => $contact
            ]);
        }

        return $this->render('contact/index.html.twig', [
            'ContactForm' => $contactForm->createView(),
        ]);
    }

Simple me thought I just can use contact.time_added in the twig-template, but this trows the following: enter image description here

I dumped contact in twig and got the following:

/var/www/html/vendor/twig/twig/src/Extension/DebugExtension.php:59:
object(App\Entity\Contact)[437]
  private 'id' => null
  private 'name' => string 'Markus' (length=6)
  private 'email' => string 'test@test.de' (length=12)
  private 'subject' => string 'Testanfrage' (length=11)
  private 'text' => string 'SVasdfasdfasdfas dfqwertcqet vsdv awadf a' (length=41)
  private 'time_added' => 
    object(DateTime)[762]
      public 'date' => string '2020-12-23 19:08:26.047662' (length=26)
      public 'timezone_type' => int 3
      public 'timezone' => string 'Europe/Berlin' (length=13)
  private 'update_time' => null

So there I saw time_added is another object. My idea was now to try it like this:

{{ contact.time_added.date }}

But I get the same result.

Can someone please point me in the right direction how to access the date?

Mark
  • 69
  • 10

2 Answers2

2

Insuring you have a getter method as follows:

public function getTimeAdded() {
    return $this->time_added;
}

The correct syntax to render the DateTime in Twig would be as below:

{{ contact.timeAdded|date }}

{# optionally pass a DateTime format string #}
{{ contact.timeAdded|date('m/d/Y') }}

Output should be:

December 23, 2020 19:08

12/23/2020

https://twig.symfony.com/doc/3.x/filters/date.html

Arleigh Hix
  • 9,990
  • 1
  • 14
  • 31
  • I have a getter in the entity `public function getTimeAdded(): ?\DateTimeInterface { return $this->time_added; }` I really have no clue why it's not working. Maybe because of the Interface in the function? – Mark Dec 23 '20 at 21:20
-1

I figured it out and I feel so stupid for not seeing it.
It's {{ contact.timeadded|date }} and not {{ contact.time_added|date }}

Sorry for bothering you :(

Mark
  • 69
  • 10