Symfony the form does not save

0

I commented that I am developing a form in symfony 3.4, the form is rendered correctly and displayed on the screen. The problem happens when clicking on the input submit nothing happens when I should save in the database. The code to save what is entered in the form is:

   public function agregar(Request $request)
{
    $persona = new Persona();
    $formulario = $this->createForm(PersonaType::class,$persona);
 //   $this->render('base.html.twig',array("persona"=>$formulario->createView()));
    $formulario->handleRequest($request);
    if($formulario -> isSubmitted() && $formulario->isValid())
    {
        $persona = $formulario->getData();

        $db = $this->getDoctrine()->getManager();
        $db->persist($persona);
        $db->flush();

        return new Response('<html>hola </html>');
    }
    else
    {
        return new Response('<html>chau </html>');
    }
}

I show it in the following way:

     /**
 * @Route("/post", name="post_list")
 */
public function mostar()
{
    $formulario = $this->createForm(PersonaType::class);
    return $this->render('base.html.twig',array("persona"=>$formulario->createView()));
}

In twig

    <h2>Formulario con Symfony3</h2>
{{form(persona)}}
{{form_end(persona)}}

Try to associate a route to the add () method but it did not work and I'm honestly lost of how to make it work, I await your answers. Greetings

    
asked by Cristian Budzicz 22.03.2018 в 03:14
source

1 answer

0

If you are going to use the form form you do not have to use the form_end, and if you are going to use form_end you must start with form_start. You must indicate a name to your add action, in this example I assume that it is add_person, just to give you a sample of how to do it, in order your twig should look like this:

{{ form(persona, {'action': path('agregar_persona')}) }}

O

{{ form_start(persona, {'action': path('agregar_persona')}) }}
{{ form_end(persona) }}

Although form_end renders all your form fields to you, it is advisable to use form_widget according to the Symfony official page link , that is:

{{ form_start(persona, {'action': path('agregar_persona')}) }}
{{ form_widget(persona) }}
{{ form_end(persona) }}
    
answered by 22.03.2018 / 14:15
source