php symfony controller doesn't get the id but the object while calling only the id
Get the solution ↓↓↓I have a form that I submit and I parse the data through controller.
I want to extract the Id to pass it to my route and delete that related item. I use form->getData() and when I pass the variable to which I assigned this form->getData() it returns an error message (Warning: preg_match() expects parameter 2 to be string, object given)..., here is my controller:
public function newGroupAction(Request $request)
{
$group = new TricksGroup();
$form = $this->createForm(TricksGroupType::class, $group);
$form->handleRequest($request);
here is the issue:
**if($form->getClickedButton() && 'Remove' === $form->getClickedButton()->getName()) {
$id = $form->get('groupId')->getData();
dump($id);
return $this->redirectToRoute('oc_prep_delgroup', array('id' => $id));**
}
here is fine, the issue is the code above
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($group);
$em->flush();
$this->addFlash('success', 'Group created successfully!');
return $this->redirectToRoute('oc_prep_add');
}
return $this->render('OCPrepBundle:Default:newgroup.html.twig', [
'form' => $form->createView(),
]);
}
I dumped the $id to see what it brings and indeed the object is assigned to it... while I just want this id: here is the print screen
Thanks in advance for your help
Answer
Solution:
Form getData method should return the form's submitted values, so then you need to call the getter for the id... something along the lines of:
$id = $form->getData()->getGroupId();
Answer
Solution:
Alright so I found the solution here. I first had to call the method $form->getData()->getGroupId(); and passing it to an array:
$id[] = $form->getData()->getGroupId();
Then I looped through the array to get the value of the groupId that I was looking for and then I passed the result in my route to generate the related url.
if($form->getClickedButton() && 'Remove' === $form->getClickedButton()->getName()) {
if (!$form->isSubmitted() || !$form->isValid()){
$this->addFlash('error', 'Invalid form please try again!');
}
$id[] = $form->getData()->getGroupId();
foreach ($id as $idreturned){
$x = $idreturned->getGroupId();
}
return $this->redirectToRoute('oc_prep_delgroup', array('id' => $x));
}
Now if anyone as an idea to optimize it I'll be happy to hear from you. But that's weird though that this $form->getData()->getGroupId(); returns the full object still don't really understand to be really honest... If anyone can explain I'll be happy to approve your answer even though I found the solution to my initial question :)
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: foreach() argument must be of type array|object, null given
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.