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');
}
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.
somehow it already updated the status before entering the //status chage if statement. I really don`t undersand why ..
This is good,we are on the right track. Now make one if with isSubmitted() and one with isValid() (not one with both). My guess is that you enter once, you change it and after you change it again. So you just see the last change. If this is the cases you need one if inside another to make it work correctly and fix this bug.
thank you so so much :) you are a lifesaver !!