Question

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 %}
Was it helpful?

Solution

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.

OTHER TIPS

Try to change this function

public function getName()
{
    return "oww_yiss_it_works";
}

this is a wild guess ( I believe that the constructor isn't working as you expect it to work ) I'm not that good with symfony, try to go back to what you have changed and you will get where the problem is

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top