SteamyVino
SteamyVino

Reputation: 21

Symfony form do not submit data - POST request is empty

I am new to symfony and twig templates and I am struggling with a problem of submiting data using form created by formbuilder in Symfony. Clicking "create" button does not submit data. I created a template addEmployee.html.twig which is extending base template with navbar and sidebar.

I noticed that this form works only if I am not extending base. So it will create proper request and submit the form only if the template in which the form is created doesnt have any parent template.

I dont know why extending base.html.twig template causing loosing of the request and I have no idea how to get it work with extending templates I have already builded.

newEmployee Function from MainController.php

  /**
     * @Route("/employee/add", name="add_employee")
     * @Method({"GET","POST"})
     */
    public function newEmployee(Request $request){


      $employee = new Employee();

      $form = $this->createFormBuilder($employee)
          ->setMethod('POST')
          ->add('employeeNumber',TextType::class, array('attr' => array('class'=>'form-control')))
          ->add('name',TextType::class, array('attr' => array('class'=>'form-control')))
          ->add('phone',TextType::class, array('attr' => array('class'=>'form-control')))
          ->add('pool',TextType::class, array('attr' => array('class'=>'form-control')))
          ->add('save',SubmitType::class, array('label'=>'Create','attr'=>array('class'=> 'btn btn-primary mt-3')))
          ->getForm();

          if ($request->isMethod('POST')) {
            $form->submit($request->request->get($form->getName()));
          }


      if($form->isSubmitted() && $form->isValid()){

          $employee=$form->getData();

          $entityManager= $this->getDoctrine()->getManager();
          $entityManager->persist($employee);
          $entityManager->flush();

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

          return $this->render('/Employee/addEmployee.html.twig',array('form'=>$form->createView()));

  }

addEmployee.html.twig'

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

{% block title%} New Article{% endblock %}

{% block body%}

{{form_start(form)}}
{{form_widget(form)}}
{{form_end(form)}}

{% endblock %}

base.html.twig

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>{% block title %}TL Manage{% endblock %}</title>        
        {% block stylesheets %}
        <link rel="stylesheet" href="https://bootswatch.com/4/cosmo/bootstrap.min.css">
        <link rel="stylesheet" href="../css/style.css">
        {% endblock %}
    </head>
    <body>

    {% include 'inc/navbar.html.twig' %}
    {% include  'inc/sidebar.html.twig' %}
     <div id="content">
            {% block body %}{% endblock %}
     </div>

    {% block javascript %}
    <!-- Icons.JS -->
    <script src="https://unpkg.com/[email protected]/dist/ionicons.js"></script>
    <!-- JQUERY -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <!-- Popper.JS -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ" crossorigin="anonymous"></script>
    <!-- Bootstrap JS -->
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script>
    <!-- jQuery Custom Scroller CDN -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/malihu-custom-scrollbar-plugin/3.1.5/jquery.mCustomScrollbar.concat.min.js"></script>
    <script src="js/main.js"></script>
     {% endblock %}
    </body>
</html>

Instead of redirecting to homepage and inserting data this is what happenes if I submit the form

and after removing {% extends 'base.html.twig' %} from addEmployee.html.twig' form works as intended. Any ideas how to get it work with the rest of the ui parts?

Upvotes: 1

Views: 3280

Answers (1)

SteamyVino
SteamyVino

Reputation: 21

I found a cause and solution by myself and I feel embarassed...

It turns out I had another form in sidebar which was included in base template and I did not close <form> tag... so it was like this:

inc/sidebar.html.twig

 <form class="employee-Finder">
       <input class="finder-input" type="text">
 <form> <!-- this was the cause -->

So the submit button from the first form was probably submitting the one in the sidebar which by default has get method. After closing the tag all works fine. Thank You for all your suggestions.

Upvotes: 1

Related Questions