Warm tip: This article is reproduced from serverfault.com, please click

Symfony : how to check multiple string types in the controller on edit function

发布于 2020-11-30 00:28:34

I have the UserTasksController where I want to edit multiple fields when editing in the form. For example : if user1 has status1 on his task and that status is changed with status2 , i want to update the "change_status_date" and "change_status_by". I tried to do that with boolean types and it worked, but i cant seem to figure it out here. The status is changed to status2,but the fields of interest are not ("change_status_date" and "change_status_by"). I want to do this with multiple fileds, like priority or task category . please heelp . Here is the edit function in the Controller

    public function edit(Request $request, $id): Response
    {
        $userTask = new UserTasks();
        $userTask = $this->getDoctrine()->getRepository(UserTasks::class)->find($id);
        $form = $this->createFormBuilder($userTask)
            ->add('name')
            ->add('description')
            ->add('completed', CheckboxType::class, [
                'required' => false,
                'label_attr' => ['class' => 'checkbox-custom ',
                ],
            ])
            ->add('active', CheckboxType::class, [
                'required' => false,
                'label_attr' => ['class' => 'checkbox-custom ',
                ],
            ])
            ->add('due_date', DateType::class, [
                'widget' => 'choice',
            ])
            ->add('category', EntityType::class, [
                'class' => TaskCategories::class
            ])
            ->add('task_type', EntityType::class, [
                'class' => TaskTypes::class
            ])
            ->add('status', EntityType::class, [
                'class' => TaskStatuses::class
            ])
            ->add('priority', EntityType::class, [
                'class' => TaskPriority::class
            ])
            ->add('created_for', EntityType::class, [
                'class' => Users::class
            ])


            ->getForm();


        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $data = $form->getData();
            $userTask->setLastUpdateDate(new \DateTime());
            $userTask->setLastUpdatedBy($this->getUser());
//            active change
            if ($userTask->getActive() == '0') {
                $userTask->setDeleted('1');
                $userTask->setVisible('0');
            }
//            completed changes
            if ($userTask->getCompleted() == '0') {
                $userTask->setDeleted('0');
                $userTask->setVisible('1');
                $userTask->setActive('1');

            }
            //status change
            if( $userTask->getStatus() != $form->getData()->getStatus () ){
                $userTask->setChangeStatusDate(new \DateTime());
                $userTask->setChangeStatusBy($this->getUser());
            }

            $em = $this->getDoctrine()->getManager();
            $em->persist($userTask);
            $em->flush();

            return $this->redirectToRoute('user_tasks_index');
        }
Questioner
ioana
Viewed
0
Kolovos Konstantinos 2020-11-30 19:40:09

Your code does not appear to have a problem to me.

Can you confirm that it enters in the //status change if statement maybe with a echo? If never enter in if then dump the userTask->getStatus() and $form->getData()->getStatus() values before to see why this happend.

If it does enter can you dump the $userTask before the persist() to confirm that is not take the value you insert them? If they take the values in the $userTask object then you have problem from the database insert (the flush() method) and you should see the error logs for more details.