抬头 - 我已经here了。 Symfony3形式的奇怪问题。所以我创建了FormType类:
class GetPostType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->setMethod("POST")
->add('phrase', Type\TextType::class);
}
}
如您所见,表单不受任何实体或其他对象的约束(尽管这应该无关紧要)。现在,当我尝试使用该表单处理传入请求时:
$postForm = $this->createForm(GetPostType::class);
$postForm->handleRequest($this->request);
...通常handleRequest应该提交我的表格。但是:
$postForm->isSubmitted();
是假的。
我已将问题来源追溯到 HttpFoundationRequestHandler 。
} elseif ($request->request->has($name) || $request->files->has($name)) {
据此,我的表格名称不在请求中。这就是我没有得到的。我在工作中使用Symfony表单,没有问题。我在不同的项目中使用它们,这没关系。现在由于某种原因,我不能让他们工作。这可能是显而易见的事情,但我想我需要另外一双眼睛才能看到它。
正如您所看到的,我确实尝试手动设置表单方法(默认情况下应该是POST)但它不起作用。我确实清除了缓存和内容。我确实尝试过在互联网上找到的不同的东西,但没有运气。
我找不到任何可能的原因。任何想法可能是什么?
更新
根据评论部分的要求,我提供了我的请求对象的转储。 那么我怎么知道这是正确的呢?它被handleRequest方法接受,它希望请求类型包含正确的数据。
/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
答案 0 :(得分:1)
表单呈现的实际字段名为get_post[phrase]
,而不仅仅是phrase
。在邮递员中提交get_post[phrase]
将有效。
如果您不想要这种命名方案,可以覆盖表单中的getBlockPrefix
方法,例如:
<?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;
}
}