Reputation: 8548
In symfony 3 this code below worked fine. Upgrading to symfony 5 it doesn't.
Controller:
public function deleteAction(Request $request, ACRGroup $aCRGroup)
{
$form = $this->createDeleteForm($aCRGroup);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//Check for related entities (if found, prevent deletion)
$reasons ="";
//Users
if (!$aCRGroup->getUsers()->isEmpty()) {
$reasons .= 'användare ';
}
//DebitPeriods
if (!$aCRGroup->getDebitPeriods()->isEmpty()) {
$reasons .= 'debiteringsperioder ';
}
//Analysis
if (!$aCRGroup->getAnalysis()->isEmpty()) {
$reasons .= 'analyser(flygsäkerhetsbedömningar) ';
}
//Positions
if (!$aCRGroup->getPositions()->isEmpty()) {
$reasons .= 'positioner ';
}
if (strlen($reasons)>0) {
$this->helper->flash('fail','Kunde inte radera gruppen '.$aCRGroup->getName().' eftersom det finns '.$reasons.' knutna till den.');
return $this->redirectToRoute('group_index');
} else {
$groupName = $aCRGroup->getName();
$em = $this->em;
$em->remove($aCRGroup);
$em->flush($aCRGroup);
$this->helper->flash('success','Raderade gruppen '.$groupName);
return $this->redirectToRoute('group_index');
}
}
Form:
*/
private function createDeleteForm(ACRGroup $aCRGroup)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('group_delete', array('id' => $aCRGroup->getId())))
->setMethod('DELETE')
->getForm()
;
}
The form looks like this when rendered
<form name="form" method="post" action="/webtools/public/group/20/delete">
<input type="hidden" name="_method" value="DELETE">
<input class="btn btn-danger pull-right" type="submit" value="Ta bort Grupp" onclick="return confirm('Är du säker att du vill ta bort denna Grupp?')">
<input type="hidden" id="form__token" name="form[_token]" value="31b74d.RTs_QX5H9-4T_S5iQXKLD7G4z_3sQI_6TM_Xepg3rYY.NmQNbCdqnatjsxsOcUXgftD_roiZAeW7C_iiIsEFgPYxCXMtJiXClEmnHg">
</form>
Routing:
group_delete:
path: /{id}/delete
defaults: { _controller: App\Controller\ACRGroupController::deleteAction }
methods: DELETE
This results in error
no route found for "POST http://localhost:8888/webtools/public/group/20/delete": Method Not Allowed (Allow: DELETE)
Changing the routing to methods: POST ...
group_delete:
path: /{id}/delete
defaults: { _controller: App\Controller\ACRGroupController::deleteAction }
methods: POST
... that allows the code to step into the controller, but then the test for
if ($form->isSubmitted() && $form->isValid())
always returns false. A dd($form) reveals that form isSubmitted indeed equals false.
Apparently that was due to the conflict between the routing expecting POST and the CreateDeleteForm generating a form with method DELETE. So changing the form generating function to match POST...
->setMethod('DELETE')
... makes it all work again.
But why is DELETE seemingly no longer allowed? Did I miss something here, isn't the DELETE method standard by now?
Upvotes: 0
Views: 412
Reputation: 81
What I've found is in my case when upgrading to Symfony 5 the property http_method_override: true
was set to true by the recipe—I didn't check right when updating the recipe, so I removed the config, because they have updated it with the false
value by default.
https://github.com/symfony/symfony/issues/45278
Upvotes: 0