1

hola he estado revisando este crud que se genera automaticamente y hay cosas que no entiendo. Por ejemplo, al hacer click en

<a href="{{ path('personas_edit', {'id': persona.id}) }}">edit</a>

¿hace un submit del formulario? Lo pregunto porque probe a eliminar de la ruta el metodo post (dejando solo el get) y me da un error.(No route found for "POST /personas/4/edit": Method Not Allowed (Allow: GET)). Es que parece que pasa el id por get (osea por url) pero en cambio no me funciona. Este es el controlador:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use App\Repository\MyClassRepository;
use App\Entity\Tablaform;
use App\Form\Formulario2Type;
/**
 * @Route("/personas")
 */

class ControladorFormularios2Controller extends AbstractController
{
    /**
     * @Route("/", name="personas_index", methods={"GET"})
     */
    public function index(MyClassRepository $myClassRepository): Response
    {
        return $this->render('controlador_formularios2/index.html.twig', [
            'personas' => $myClassRepository->findAll(),
        ]);
    }
    
    /**
     * @Route("/new", name="persona_new", methods={"GET","POST"})
     */
    
    public function new(Request $request): Response
    {
        $tablaForm = new Tablaform();
        $form = $this->createForm(Formulario2Type::class, $tablaForm);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($tablaForm);
            $entityManager->flush();

            return $this->redirectToRoute('personas_index');
        }

        return $this->render('controlador_formularios2/new.html.twig', [
            'tablaForm' => $tablaForm,
            'form' => $form->createView(),
        ]);
    }
    
    /**
     * @Route("/{id}/delete", name="personas_delete", methods={"GET","POST"})
     */
    
    public function delete(Request $request, Tablaform $tablaForm): Response
    {
        //if ($this->isCsrfTokenValid('delete'.$tablaForm->getId(), $request->request->get('_token'))) 
        {
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->remove($tablaForm);
            $entityManager->flush();
        }

        return $this->redirectToRoute('personas_index');
    }
    
     /**
     * @Route("/{id}/edit", name="personas_edit", methods={"GET","POST"})
     */
    public function edit(Request $request, Tablaform $tablaForm): Response
    {
        $form = $this->createForm(Formulario2Type::class, $tablaForm);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->getDoctrine()->getManager()->flush();

            return $this->redirectToRoute('personas_index');
        }

        return $this->render('controlador_formularios2/edit.html.twig', [
            'tablaForm' => $tablaForm,
            'form' => $form->createView(),
        ]);
    }

y esta es la vista:

{% extends 'base.html.twig' %}

{% block title %}Listado{% endblock %}

{% block body %}
    <table class="table">
        <thead>
            <tr>
                <th>Id</th>
                <th>Nombre</th>
                <th>Edad</th>
            </tr>
        </thead>
        <tbody>
        {% for persona in personas %}
            <tr>
                <td>{{ persona.id }}</td>
                <td>{{ persona.nombre }}</td>
                <td>{{ persona.edad }}</td>
               <td>
                    {{ include('controlador_formularios2/_delete_form.html.twig') }}
                    <a href="{{ path('personas_edit', {'id': persona.id}) }}">edit</a>
                </td>
            </tr>
        {% else %}
            <tr>
                <td colspan="4">no records found</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
     <a href="{{ path('persona_new') }}">Create new</a>   
{% endblock %}

1 respuesta 1

1

No, el formulario no hace submit, esto lo puedes comprobar poniendo lo siguiente:

var_dump($form->isSubmitted()); // true o false

Esto te indica si el formulario ha sido enviado, es decir, que hayan clickado en el botón submit del formulario.

Necesitas que el método admita tanto POST como GET dado que se usan ambos métodos tanto para la recogida de la información de la persona que se va a editar, como para luego guardar las modificaciones en la BD.

El ID de la persona a editar no se pasa por GET, de ser así, la URL sería la siguiente:

/personas/edit?{ID}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.