Symfony2 form: unexpected "no POST options"

I am trying to save an object through a Symfony Form component. He does not work.

When submitting the form, Symfony redirects me to the actual form, with the data previously provided in the URL using the GET method. In Profiler, I can see that "there are no POST options."

Controller:

<?php

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Quest;
use AppBundle\Form\QuestType;

class QuestController extends Controller
{
\\...
    /**
     * @Route("/admin/quest/add", name="quest_add")
     */
  public function addAction(Request $request)
  {
    $quest = new Quest();
    $form = $this->createForm(QuestType::class, $quest);
    $form->handleRequest($request);

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

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

    return $this->render('admin/quest/add.html.twig', array(
      'form' => $form->createView(),
    ));
  }
}

A type:

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;

class QuestType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
        ->add('questName',      TextType::class)
        ->add('questCost',      IntegerType::class)
        ->add('nbEnigma',       IntegerType::class)
        ->add('dateStart',      DateTimeType::class)
        ->add('dateEnd',        DateTimeType::class)
        ->add('rewardFinish',   IntegerType::class)
        ->add('rewardFurther',  IntegerType::class)
        ->add('speech',         TextareaType::class)
        ;
    }
\\...
}

View:

/* ... */
<div class="well">
  {{ form_start(form) }}
  {{ form_errors(form) }}

  {{ form_row(form.questName) }}
  /* ... */

  <input type="submit" value="Create" method="post" class="btn btn-default pull-right" />

  {{ form_end(form) }}
</div>
/* ... */

Please note that I do not use validation at this time.

The object that I use is in order, the corresponding table was created in my PostgreSQL database through the Symfony console, the .yml settings are also in order. I can place these elements if necessary.

So what could go wrong? Is this a problem in my installation or configuring Wamp, for example, for example in Apache? The fact is, I don’t know where the problem is.

, -. PHP, Symfony2.


1) HTML , , Twig, "POST": <form name="quest" method="post">

2) @Method({"GET", "POST"}) @Route use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; .

3.1) :

  • Wampserver 2.5 (32 PHP 5.5.12, Apache 2.4.9)
  • PostgreSQL 9.3.2
  • Symfony 2.8.1, + FOSUserBundle ~2.0@dev
  • Doctrine2
  • SuperAdmin ( -, /admin) dev (app_dev.php).

3.2) , - ( Symfony, openclassrooms.com dynamic-mess.com), .

4) , , , Symfony Wampserver PostgreSQL. , . .

5) HTML- : ( )

:

<?php

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type\TextType;

class QuestController extends Controller
{

// ...

    /**
     * @Route("/admin/test/add", name="test")
     */
    public function testAction(Request $request)
    {
        $defaultData = array('message' => 'Type your message here');
        $form = $this->createFormBuilder($defaultData)
            ->add('nametest', TextType::class)
            ->getForm();

        $form->handleRequest($request);

        if ($form->isValid()) {
            $data = $form->getData();
        }

    return $this->render('admin/test.html.twig', array(
      'form' => $form->createView(),
     ));
    }
}

:

/* ... */

<div class="well">
  {{ form_start(form) }}
  {{ form_errors(form) }}

  {{ form_row(form.nametest) }}
  <input type="submit" value="Create" method="post" class="btn btn-default pull-right" />

  {{ app.request }}

  {{ form_end(form) }}
</div>

/* ... */

Twig {{ app.request }}.

:

GET /gowinquest/web/app_dev.php/admin/test/add HTTP/1.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Encoding: gzip, deflate Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3 Cache-Control: max-age=0 Connection: keep-alive Cookie: __utma=111872281.2124856035.1451232730.1452442012.1452447222.32; __utmz=111872281.1451232730.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmc=111872281; PPA_ID=not8odr8dlslv6eoo25l2llai6; webfx-tree-cookie-persistence=wfxt-14+wfxt-36+wfxt-20+wfxt-4+wfxt-16+wfxt-18+wfxt-6; PHPSESSID=h94nvh0nn315po8np66httkv56; __utmb=111872281.0.10.1452447222 Host: localhost User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0 X-Php-Ob-Level: 1 

:

GET /gowinquest/web/app_dev.php/admin/test/add?form%5Bnametest%5D=blablabla&form%5B_token%5D=pzhE6hrzsNf3EYbHJLmEXf0miAh4J5jeOH_ccrmlmW4 HTTP/1.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Encoding: gzip, deflate Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3 Connection: keep-alive Cookie: __utma=111872281.2124856035.1451232730.1452442012.1452447222.32; __utmz=111872281.1451232730.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmc=111872281; PPA_ID=not8odr8dlslv6eoo25l2llai6; webfx-tree-cookie-persistence=wfxt-14+wfxt-36+wfxt-20+wfxt-4+wfxt-16+wfxt-18+wfxt-6; PHPSESSID=h94nvh0nn315po8np66httkv56; __utmb=111872281.0.10.1452447222 Host: localhost Referer: http://localhost/gowinquest/web/app_dev.php/admin/test/add User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0 X-Php-Ob-Level: 1  

6) Symfony CRUD Entity, IT WORKED! POST .

, , Controller Form by Symfony, , ...

CRUD @Method({"GET", "POST"}), , . , @Method (. )...

, PHP Symfony, , - , ... ? . , - ? .

7) , Symfony2.8 app/logs/dev.log, , , :

[2016-01-11 16:02:38] request.INFO: Matched route "_wdt". {"route_parameters":{"_controller":"web_profiler.controller.profiler:toolbarAction","token":"e945fe","_route":"_wdt"},"request_uri":"http://localhost/gowinquest/web/app_dev.php/_wdt/e945fe"} []
[2016-01-11 16:03:16] request.INFO: Matched route "quest_add". {"route_parameters":{"_controller":"AppBundle\\Controller\\QuestController::addAction","_route":"quest_add"},"request_uri":"http://localhost/gowinquest/web/app_dev.php/admin/quest/add?quest%5B_token%5D=74fPFHnb4NPKfAotccGEE9T4EDCUhaEHY4w-hqViwUw&quest%5BdateEnd%5D%5Bdate%5D%5Bday%5D=1&quest%5BdateEnd%5D%5Bdate%5D%5Bmonth%5D=1&quest%5BdateEnd%5D%5Bdate%5D%5Byear%5D=2011&quest%5BdateEnd%5D%5Btime%5D%5Bhour%5D=0&quest%5BdateEnd%5D%5Btime%5D%5Bminute%5D=0&quest%5BdateStart%5D%5Bdate%5D%5Bday%5D=1&quest%5BdateStart%5D%5Bdate%5D%5Bmonth%5D=1&quest%5BdateStart%5D%5Bdate%5D%5Byear%5D=2011&quest%5BdateStart%5D%5Btime%5D%5Bhour%5D=0&quest%5BdateStart%5D%5Btime%5D%5Bminute%5D=0&quest%5BnbEnigma%5D=1&quest%5BquestCost%5D=1&quest%5BquestName%5D=Test4&quest%5BrewardFinish%5D=1&quest%5BrewardFurther%5D=1&quest%5Bspeech%5D=test4speech"} []
[2016-01-11 16:03:16] security.DEBUG: Read existing security token from the session. {"key":"_security_main"} []
[2016-01-11 16:03:16] doctrine.DEBUG: SELECT t0.username AS username_1, t0.username_canonical AS username_canonical_2, t0.email AS email_3, t0.email_canonical AS email_canonical_4, t0.enabled AS enabled_5, t0.salt AS salt_6, t0.password AS password_7, t0.last_login AS last_login_8, t0.locked AS locked_9, t0.expired AS expired_10, t0.expires_at AS expires_at_11, t0.confirmation_token AS confirmation_token_12, t0.password_requested_at AS password_requested_at_13, t0.roles AS roles_14, t0.credentials_expired AS credentials_expired_15, t0.credentials_expire_at AS credentials_expire_at_16, t0.id AS id_17 FROM fos_user t0 WHERE t0.id = ? LIMIT 1 [1] []
[2016-01-11 16:03:16] security.DEBUG: User was reloaded from a user provider. {"username":"admin","provider":"FOS\\UserBundle\\Security\\EmailUserProvider"} []
[2016-01-11 16:03:17] security.DEBUG: Stored the security token in the session. {"key":"_security_main"} []
[2016-01-11 16:03:18] request.INFO: Matched route "_wdt". {"route_parameters":{"_controller":"web_profiler.controller.profiler:toolbarAction","token":"83737b","_route":"_wdt"},"request_uri":"http://localhost/gowinquest/web/app_dev.php/_wdt/83737b"} []
Hide result

Profiler ( ), :

200 ::1 GET http://localhost/gowinquest/web/app_dev.php/admin/quest/add?quest%5B_token%5D=74fPFHnb4NPKfAotccGEE9T4EDCUhaEHY4w-hqViwUw&quest%5BdateEnd%5D%5Bdate%5D%5Bday%5D=1&quest%5BdateEnd%5D%5Bdate%5D%5Bmonth%5D=1&quest%5BdateEnd%5D%5Bdate%5D%5Byear%5D=2011&quest%5BdateEnd%5D%5Btime%5D%5Bhour%5D=0&quest%5BdateEnd%5D%5Btime%5D%5Bminute%5D=0&quest%5BdateStart%5D%5Bdate%5D%5Bday%5D=1&quest%5BdateStart%5D%5Bdate%5D%5Bmonth%5D=1&quest%5BdateStart%5D%5Bdate%5D%5Byear%5D=2011&quest%5BdateStart%5D%5Btime%5D%5Bhour%5D=0&quest%5BdateStart%5D%5Btime%5D%5Bminute%5D=0&quest%5BnbEnigma%5D=1&quest%5BquestCost%5D=1&quest%5BquestName%5D=test5&quest%5BrewardFinish%5D=1&quest%5BrewardFurther%5D=1&quest%5Bspeech%5D=test5
11-Jan-2016 16:24:32 ee563e
200 ::1 GET http://localhost/gowinquest/web/app_dev.php/admin/quest/add 11-Jan-2016 16:23:39 d0f70c
Hide result
+4

Source: https://habr.com/ru/post/1623582/


All Articles