'Exception thrown during the rendering of a template("Parameter "id" for route "url" must match "[^/]++" ("" given) to generate a corresponding URL.")
I have a delete button which is created here:
_delete_form.html.twig
<form method="post" action="{{ path('finals_delete', {'id': final.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ final.id) }}">
<button class="btn btn-success" style="margin-top: 10px">Verwijderen</button>
I include it in Edit page like so:
{{ include('finals/_form.html.twig', {'button_label': 'Opslaan'}) }}
<button class="btn btn-success"><a href="{{ path('finals_index') }}">Terug naar lijst</a> </button>
{{ include('finals/_delete_form.html.twig') }}
The controller action for Delete:
/**
* @Route("/{id}", name="finals_delete", methods={"POST"})
*/
public function delete(Request $request, Finals $final): Response
{
if ($this->isCsrfTokenValid('delete'.$final->getId(), $request->request->get('_token'))) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($final);
$entityManager->flush();
}
return $this->redirectToRoute('finals_index');
}
The controller action for edit:
/**
* @Route("/{id}/edit", name="finals_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Finals $final): Response
{
$final = new Finals();
$form = $this->createForm(FinalsType::class, $final);
$form->handleRequest($request);
$imageFile = $form->get('imageTeam1')->getData();
//If function to only process an imagine if its uploaded
if ($imageFile) {
$originalFilename = pathinfo($imageFile->getClientOriginalName(), PATHINFO_FILENAME);
//Remove unwanted characters from filename
$safeFilename = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename);
$newFilename = $safeFilename.'-'.uniqid().'.'.$imageFile->guessExtension();
//Move file to image dir
try {
$imageFile->move($this->getParameter('images_directory'),$newFilename);
} catch (FileException $e) {
$this->addFlash('danger', 'Er is iets fout gegaan probeer het opnieuw');
}
$final->setImageTeam1($newFilename);
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('finals_index');
}
return $this->render('finals/edit.html.twig', [
'final' => $final,
'form' => $form->createView(),
]);
}
All this was created using the bin/console make:crud command, I have made multiple pages like this with the exact same button and code but only this one for some reason gives this error:
An exception has been thrown during the rendering of a template ("Parameter "id" for route "finals_delete" must match "[^/]++" ("" given) to generate a corresponding URL.").
in C:\xampp\htdocs\Freulepartij\templates\finals_delete_form.html.twig (line 1)
It throw's the error as soon as I click my edit page(when the delete button is just being rendered and the function behind it not executed?) which I find strange. When I remove the include I can get in my edit page but My edit form won't update the record either so it just doesn't know what to do with the data? I am really confused because the id is right in the url when I click my edit page without the delete button
UPDATE 1
As Gary Houbre suggested I replaced
{{ include('finals/_delete_form.html.twig' }}
with
{{ include('finals/_delete_form.html.twig' , {'final': final.id}) }}
And now get this error :
Impossible to access an attribute ("id") on a null variable.
But I am looking in my database and only have 2 records in this table both have id's so I dont know how it is getting a null value?
UPDATE 2
I found this in the symfony logs which confirms the right id "7" So thats leaving me with even more confusion..
SELECT t0.id AS id_1, t0.team1 AS team1_2, t0.team2 AS team2_3, t0.image_team1 AS image_team1_4, t0.image_team2 AS image_team2_5, t0.paragraph AS paragraph_6, t0.eersten_p1 AS eersten_p1_7, t0.eersten_p2 AS eersten_p2_8, t0.punten_p1 AS punten_p1_9, t0.punten_p2 AS punten_p2_10 FROM finals t0 WHERE t0.id = ?
[ "7" ]
Solution 1:[1]
As pointed out in gary's comments i had a new call in a edit function which obviously does not belong there so thats where my problem was
Solution 2:[2]
Into your edit.html.twig, the line where your include file _delete.html.twig
Your need add parameter 'id'. For example :
{% include 'template.html' with {'foo': 'bar'} %}
I follow this documentation Twig in this link
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Romeo Beun |
Solution 2 | Gary Houbre |