首页 > 解决方案 > Symfony 表单 - 数据时间添加 +30 分钟

问题描述

当我在表单中设置 beginAt(DateTime) 时,我想在“endAt”中设置相同的日期,但 +30 分钟。我不知道该怎么做:(

第一个代码是我项目中的部分表单,第二个代码是添加约会的功能“新约会”。

$builder
                ->add('title', TextType::class, ['label'=>'Tytuł'])
                ->add('description', TextType::class, ['label'=>'Treść'])
                ->add('beginAt')
                ->add('endAt')


            ;



public function new(Request $request, $id, TokenStorageInterface $tokenStorage): Response
    {
        $currentUser = $tokenStorage->getToken()
                ->getUser();

        $username = $currentUser->getUsername();

        $appointment = new Appointments();
        $appointment->setDoctor($id);
        $appointment->setUsername($username);
        $form = $this->createForm(AppointmentsType::class, $appointment);

        $form->handleRequest($request);


        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($appointment);
            $em->flush();

            return $this->redirectToRoute('przychodnia_index');
        }


        return $this->render('appointments/new.html.twig', [
            'appointment' => $appointment,
            'form' => $form->createView(),
        ]);
    }

标签: formssymfony

解决方案


例如,尝试使用Symfony 表单事件来更新 endAt 字段,并使用一些 ajax 动态提交表单。这里有一个很棒的教程(法语对不起)https://www.grafikart.fr/tutoriels/champs-imbriques-888


推荐阅读