M
M
Maxim Savichev2015-09-17 20:26:31
symfony
Maxim Savichev, 2015-09-17 20:26:31

How can I make the form submit the current date when filled out?

Good afternoon everyone.
Today I've been trying all day to get the current date value to be written to the database when submitting the form.
The only option turned out was when the date began to record the value "2010-01-01"
I could not do it differently.
And now to what I have:
A separate file with a form:

class DonutType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            //Создаем форму
            $builder->add('date_operation', 'date', array('data' => $options['date_option']))
                ->getForm();
        }
    
        public function getName()
        {
            return 'donut';
        }
    
        public function configureOptions(OptionsResolver $resolver)
        {
            $date_option = new \Date;
            $date_option = date_format($date_option, 'yyyy-MM-dd');
            $resolver->setDefaults(array(
                'data_class' => 'M4\MinecraftBundle\Entity\Donut',
                'date_option' => $date_option,
            ));
        }
    }

And the controller itself:
public function donutAction(Request $request, $donut_success = 0){

        //$date_option = new \DateTime('tomorrow');
        //$date_option = date_format($date_option, 'yyyy-MM-dd');

        $id_user= $this->get('security.context')->getToken()->getUser()->getId();
        $donut = new Donut();
        $form = $this->createForm(new DonutType(), $donut, array('id_user'=>$id_user, 'sum'=>1));


            if ($request->getMethod() == 'POST') {
                $form->bind($request);

                if ($form->isValid()) {

                    // выполняем прочие действие, например, сохраняем задачу в базе данных donut
                    $em = $this->getDoctrine()->getEntityManager();
                    $em->persist($donut);
                    $em->flush();

                    return $this->redirect($this->generateUrl('m4_minecraft_homepage'));
                }
            }

        return $this->render('M4MinecraftBundle:Default:donut.html.twig', array(
            'form' => $form->createView(),
        ));
    }

Previously, the date was calmly sent, but after I decided to make the form in a separate file, such problems began. Can you tell me how I can pass the current date to the query?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ostin, 2015-09-17
@MaksimSa

You do not need to take the date from the form, but directly write it in the controller to the model.

$donut = new Donut();
$donut->date = new \DateTime();

A
Alexey Skobkin, 2015-09-17
@skobkin

You can simply fill in the property with the desired date in the Donut constructor. Or use, for example, Timestampable from DoctrineExtensions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question