Robert
Robert

Reputation: 1276

Symfony3.3 form name not in Request

Heads up - I've already been here. Strange issue with Symfony3 forms. So I've created FormType class:

class GetPostType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->setMethod("POST")
            ->add('phrase', Type\TextType::class);
    }
} 

As you can see form is not bound to any entity or other object(this shouldn't matter though). Now when I try to handle incoming request with that form:

$postForm = $this->createForm(GetPostType::class);
$postForm->handleRequest($this->request);

... normally handleRequest should submit my form. However:

$postForm->isSubmitted();

is false.

I've tracked down source of issue to HttpFoundationRequestHandler.

} elseif ($request->request->has($name) || $request->files->has($name)) {

According to this my form name is not present in request. That's what I don't get. I'm using Symfony forms at work and there's no problem. I used them in different projects and it was ok. Now for some reason I can't get them to work. It is probably something obvious but I guess I need another pair of eyes to see it.

As you can see I did try to set form method manually(it should be POST by default) but it didn't work. I did clear the cache and stuff. I did try different things I found on the internet but no luck.

I can't find anything that might be the reason. Any ideas what it might be?

UPDATE:

As requested in comments section I provide dump of my request object. So how do I know it is the right thing? It is accepted by handleRequest method witch expects request type and it contains correct data.

/var/www/story/src/CodeCraft/BlogBundle/Controller/Api/PostController.php:73:
object(Symfony\Component\HttpFoundation\Request)[49]
  public 'attributes' => 
    object(Symfony\Component\HttpFoundation\ParameterBag)[12]
      protected 'parameters' => 
        array (size=4)
          '_controller' => string 'CodeCraft\BlogBundle\Controller\Api\PostController::getPostsAction' (length=66)
          '_route' => string 'blog.api.posts.get' (length=18)
          '_route_params' => 
            array (size=0)
              ...
          '_firewall_context' => string 'security.firewall.map.context.main' (length=34)
  public 'request' => 
    object(Symfony\Component\HttpFoundation\ParameterBag)[10]
      protected 'parameters' => 
        array (size=1)
          'phrase' => string 'xxx' (length=3)
  public 'query' => 
    object(Symfony\Component\HttpFoundation\ParameterBag)[11]
      protected 'parameters' => 
        array (size=0)
          empty
  public 'server' => 
    object(Symfony\Component\HttpFoundation\ServerBag)[15]
      protected 'parameters' => 
        array (size=34)
          'HTTP_CACHE_CONTROL' => string 'no-cache' (length=8)
          'HTTP_POSTMAN_TOKEN' => string '2eea2285-a2f7-4ca5-a799-ea97758d7d20' (length=36)
          'CONTENT_TYPE' => string 'application/x-www-form-urlencoded' (length=33)
          'HTTP_USER_AGENT' => string 'PostmanRuntime/6.1.6' (length=20)
          'HTTP_ACCEPT' => string '*/*' (length=3)
          'HTTP_HOST' => string 'story.dev' (length=9)
          'HTTP_ACCEPT_ENCODING' => string 'gzip, deflate' (length=13)
          'CONTENT_LENGTH' => string '10' (length=2)
          'HTTP_CONNECTION' => string 'keep-alive' (length=10)
          'PATH' => string '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' (length=60)
          'SERVER_SIGNATURE' => string '<address>Apache/2.4.18 (Ubuntu) Server at story.dev Port 80</address>
' (length=70)
          'SERVER_SOFTWARE' => string 'Apache/2.4.18 (Ubuntu)' (length=22)
          'SERVER_NAME' => string 'story.dev' (length=9)
          'SERVER_ADDR' => string '192.168.33.10' (length=13)
          'SERVER_PORT' => string '80' (length=2)
          'REMOTE_ADDR' => string '192.168.33.1' (length=12)
          'DOCUMENT_ROOT' => string '/var/www/story/web' (length=18)
          'REQUEST_SCHEME' => string 'http' (length=4)
          'CONTEXT_PREFIX' => string '' (length=0)
          'CONTEXT_DOCUMENT_ROOT' => string '/var/www/story/web' (length=18)
          'SERVER_ADMIN' => string 'webmaster@localhost' (length=19)
          'SCRIPT_FILENAME' => string '/var/www/story/web/app_dev.php' (length=30)
          'REMOTE_PORT' => string '57039' (length=5)
          'GATEWAY_INTERFACE' => string 'CGI/1.1' (length=7)
          'SERVER_PROTOCOL' => string 'HTTP/1.1' (length=8)
          'REQUEST_METHOD' => string 'POST' (length=4)
          'QUERY_STRING' => string '' (length=0)
          'REQUEST_URI' => string '/app_dev.php/posts' (length=18)
          'SCRIPT_NAME' => string '/app_dev.php' (length=12)
          'PATH_INFO' => string '/posts' (length=6)
          'PATH_TRANSLATED' => string '/var/www/story/web/posts' (length=24)
          'PHP_SELF' => string '/app_dev.php/posts' (length=18)
          'REQUEST_TIME_FLOAT' => float 1509896560.734
          'REQUEST_TIME' => int 1509896560
  public 'files' => 
    object(Symfony\Component\HttpFoundation\FileBag)[14]
      protected 'parameters' => 
        array (size=0)
          empty
  public 'cookies' => 
    object(Symfony\Component\HttpFoundation\ParameterBag)[13]
      protected 'parameters' => 
        array (size=0)
          empty
  public 'headers' => 
    object(Symfony\Component\HttpFoundation\HeaderBag)[16]
      protected 'headers' => 
        array (size=10)
          'cache-control' => 
            array (size=1)
              ...
          'postman-token' => 
            array (size=1)
              ...
          'content-type' => 
            array (size=1)
              ...
          'user-agent' => 
            array (size=1)
              ...
          'accept' => 
            array (size=1)
              ...
          'host' => 
            array (size=1)
              ...
          'accept-encoding' => 
            array (size=1)
              ...
          'content-length' => 
            array (size=1)
              ...
          'connection' => 
            array (size=1)
              ...
          'x-php-ob-level' => 
            array (size=1)
              ...
      protected 'cacheControl' => 
        array (size=1)
          'no-cache' => boolean true
  protected 'content' => null
  protected 'languages' => null
  protected 'charsets' => null
  protected 'encodings' => null
  protected 'acceptableContentTypes' => null
  protected 'pathInfo' => string '/posts' (length=6)
  protected 'requestUri' => string '/app_dev.php/posts' (length=18)
  protected 'baseUrl' => string '/app_dev.php' (length=12)
  protected 'basePath' => null
  protected 'method' => string 'POST' (length=4)
  protected 'format' => null
  protected 'session' => 
    object(Symfony\Component\HttpFoundation\Session\Session)[5070]
      protected 'storage' => 
        object(Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage)[5071]
          protected 'bags' => 
            array (size=2)
              ...
          protected 'started' => boolean false
          protected 'closed' => boolean false
          protected 'saveHandler' => 
            object(Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy)[5090]
              ...
          protected 'metadataBag' => 
            object(Symfony\Component\HttpFoundation\Session\Storage\MetadataBag)[5065]
              ...
      private 'flashName' => string 'flashes' (length=7)
      private 'attributeName' => string 'attributes' (length=10)
  protected 'locale' => null
  protected 'defaultLocale' => string 'en' (length=2)
  private 'isHostValid' => boolean true
  private 'isClientIpsValid' => boolean true
  private 'isForwardedValid' => boolean true

Upvotes: 0

Views: 774

Answers (1)

Kep
Kep

Reputation: 5857

The actual field that would be rendered by your form is named get_post[phrase], not just phrase. Submitting get_post[phrase] in postman will work.

If you don't want that naming scheme you can override the getBlockPrefix method in your Form, eg:

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

use Symfony\Component\Form\Extension\Core\Type\TextType;

class GetPostType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('phrase', TextType::class);
    }

    public function getBlockPrefix()
    {
        return null;
    }
}

Upvotes: 1

Related Questions