表单名称不在请求

表单名称不在请求

本文介绍了Symfony2表单不会提交。表单名称不在请求中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

找到解决方案:我没有在表单的构建器中指定一个方法。添加setMethod(POST)修复了我的问题。



我最近开始使用Symfony2(并学习TDD,猜猜我不喜欢它)自今早以来,我的表单出现问题。它曾经工作,但现在我似乎无法得到一个工作响应。



我设法找出错误一点。 Symfony获得了HttpFoundationRequestHandler类中的一个地方,它检查它是否应该处理请求(从第56行开始),第59行是它破坏的地方

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

所以如果我得到这个权利,因为它无法在请求中找到表单的名称,它只是忽略它,相信表单从未提交过。我没有得到的是,这是可能的,因为唯一可以导致页面更改/刷新的是用户按下表单的提交按钮...



如果您有任何人对我的问题可能有什么建议或简单的指示,我会非常感谢!



因为我对此非常瞎一,我会在我的TrinomeClientVideoUploadForm类和我的Controller内显示和处理表单的请求的函数下面发布。



这里如果我使用类来构建表单:

 <?php 
名称空间Trinome \WebVideoViewBundle\Model\Forms;

使用Symfony \Component\Form\AbstractType;
使用Symfony \Component\Form\FormBuilderInterface;

类TrinomeClientVideoUploadForm扩展AbstractType
{
private $ name;

public function __construct($ name =trinome_client_video_upload)
{
$ this-> name = $ name;

$ b $ **
* {@inheritdoc}
* /
public function buildForm(FormBuilderInterface $ builder,array $ options)

$ builder-> add(client,text)
- > add(video,file)
- > add(upload ,提交);

$ b / **
* {@inheritdoc}
* /
public function getName()
{
return $这个 - >名称;




$ b $ p
$ b

这是处理我的页面的函数(只)控制器:

  / ** 
* @Route(/)
* @ Template()
* /
public function indexAction(Request $ request)
{
$ uploadVideoForm = new UploadVideoFormDataSet();
$ form = $ this-> createForm(new TrinomeClientVideoUploadForm(),$ uploadVideoForm);

$ form-> handleRequest($ request);

if($ form-> isValid())
{
//它永远不会到达这里,所以我在这篇文章中删除了它。
}

返回数组(
form=> $ form-> createView(),
);
}

显示表单的Twig文件:

  {%extends@ BaseViews / base.html.twig%} 

{%block title%} Outil d'uploadvidéo Trinome {%endblock%}
{%block stylesheets%}
{%stylesheets'@ TrinomeWebVideoViewBundle / Resources / public / css / *'filter ='cssrewrite'%}
< link rel =stylesheethref ={{asset_url}}/>
{%endstylesheets%}
{%endblock%}

{%block body%}
< div id =wrapper>
< header>
{%block header%}
{%image'@logo_trinome'%}
< img src ={{asset_url}}alt =Logo Trinome/>
{%endimage%}
{%endblock%}
< / header>
< div id =content>
{%block content%}
< h1>连线< / h1>
{{form(form)}}
{%endblock%}
< / div>
< / div>
{%endblock%}


解决方案

不需要使用构造,尤其是因为您没有将控制器中的 $ name 变量作为参数传递。然后您可以删除私有财产。我不认为这是你的问题,而只是没有必要。

在您的控制器中,当您调用createForm方法时,您没有为表单传递操作或方法。我猜你的树枝正在渲染一个没有action标签的action属性。



关于你的表单类型,假设你试图绑定对象(或实体) UploadVideoFormDataSet 到表单中,那么表单类型应该看起来更像这样:

 
class TrinomeClientVideoUploadForm extends AbstractType
{
/ **
* @param FormBuilderInterface $ builder
* @param array $ options
* /
public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder
- > add(client,text)
- > add(video,文件)
- >添加(上传,提交)
;

$ b $ **
* @param OptionsResolverInterface $ resolver
* /
public function setDefaultOptions(OptionsResolverInterface $ resolver)
{
$ resolver-> setDefaults(array(
'data_class'=>'Namespace\YourBundle\Entity\UploadVideoFormDataSet'
));

$ b / **
* @return string
* /
public function getName()
{
return' namespace_yourbundle_uploadvideoformdataset;
}
}

希望这可以帮到你。


Solution found: I didn't specify a method in my form's builder. adding setMethod("POST") fixed my problem.

I've recently started using Symfony2 (and learning TDD. Guess I don't like it easy) and have been having trouble with my form since this morning. It used to work, but now I can't seem to get a working response.

I managed to pinpoint the error a bit. Symfony gets to a point in the HttpFoundationRequestHandler class where it checks if it should handle the request or not (starting at line 56) and line 59 is where it breaks

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

So if I'm getting this right, since it can't find the form's name in the Request, it simply ignores it, believing the form was never submitted. What I don't get is how that is possible since the only thing that can cause a page change/refresh is the user pressing the "Submit" button of the form...

If any of you had a suggestion or simple pointers on what my problem could be I'd be very thankful!

Since I'm feeling pretty blind on that one, I'll post below my TrinomeClientVideoUploadForm class and the function inside my Controller that displays and handles the form's request.

This right here if the class I use to build the form:

<?php
namespace Trinome\WebVideoViewBundle\Model\Forms;

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

class TrinomeClientVideoUploadForm extends AbstractType
{
    private $name;

    public function __construct( $name = "trinome_client_video_upload" )
    {
        $this->name = $name;
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm( FormBuilderInterface $builder, array $options )
    {
        $builder->add( "client", "text" )
                ->add( "video", "file" )
                ->add( "upload", "submit" );
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return $this->name;
    }
}

And this is the function that handles the page in my (only) controller:

/**
 * @Route("/")
 * @Template()
 */
public function indexAction( Request $request )
{
    $uploadVideoForm = new UploadVideoFormDataSet();
    $form = $this->createForm( new TrinomeClientVideoUploadForm(), $uploadVideoForm );

    $form->handleRequest( $request );

    if ( $form->isValid() )
    {
        // It never gets here anyways, so I removed it for this post.
    }

    return array(
        "form" => $form->createView(),
    );
}

Twig file where I render the Form:

{% extends "@BaseViews/base.html.twig" %}

{% block title %}Outil d'upload vidéo Trinome{% endblock %}
{% block stylesheets %}
    {% stylesheets '@TrinomeWebVideoViewBundle/Resources/public/css/*' filter='cssrewrite' %}
        <link rel="stylesheet" href="{{ asset_url }}" />
    {% endstylesheets %}
{% endblock %}

{% block body %}
    <div id="wrapper">
        <header>
            {% block header %}
                {% image '@logo_trinome' %}
                    <img src="{{ asset_url }}" alt="Logo Trinome" />
                {% endimage %}
            {% endblock %}
        </header>
        <div id="content">
            {% block content %}
                <h1>Connection</h1>
                {{ form( form ) }}
            {% endblock %}
        </div>
    </div>
{% endblock %}
解决方案

You shouldn't need to use a construct, especially since you aren't passing the $name variable from your controller as an argument. You can then remove the private property as well. I don't think this is your problem, rather it's just unnecessary.

In your controller when you are calling the createForm method, you aren't passing an action or a method for the form. I'm guessing your twig is rendering a form with no action attribute on the opening form tag.

Concerning your form type, assuming you are trying to tie the object (or entity) UploadVideoFormDataSet to your form, then the form type should look more like this:

class TrinomeClientVideoUploadForm extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add( "client", "text" )
            ->add( "video", "file" )
            ->add( "upload", "submit" )
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Namespace\YourBundle\Entity\UploadVideoFormDataSet'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'namespace_yourbundle_uploadvideoformdataset';
    }
}

Hope this helps you.

这篇关于Symfony2表单不会提交。表单名称不在请求中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 08:48