Etienne Noël
Etienne Noël

Reputation: 6176

Symfony2 - Creating a template for a custom form type

I have a custom form type named video_file. I'm trying to show a custom template for it, but it is still showing the default Symfony theme even though I followed every step in the documentation.

Here is my full configuration:

VideoFileType.php

<?php
# src/Acme/PhotoBundle/Form/Type/ThumbnailType.php

namespace OSC\MediaBundle\Form\Type;

use OSC\MediaBundle\Manager\VideoFileManager;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class VideoFileType extends AbstractType
{

    public $container;

    public $videoPresets = [];

    public function __construct(Container $container) {
        $this->container = $container;
        $videoPresets = $container->getParameter('osc_media.video.presets');
        foreach ($videoPresets as $key => $videoPreset) {
            array_push($this->videoPresets, $key);
        }

    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('thumbnail', 'image');
        $builder->add('file', 'file');
        $builder->add('videoPreset', 'choice', array(
            'choices' => $this->videoPresets,
            'multiple' => false,
            'required' => true
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'OSC\MediaBundle\Entity\VideoFile'
        ));
    }

    public function getDefaultOptions(array $options) {
        return array('data_class' => 'OSC\MediaBundle\Entity\VideoFile');
    }

    public function getName()
    {
        return 'video_file';
    }
}

Here's where I'm declaring the form as a service:

osc_media.form.type.video_file:
    class: %osc_media.form.type.video_file.class%
    arguments: [@service_container]
    tags:
        - { name: form.type, alias: video_file }

Simply, what I want is to override the Form template. As mentioned in the documentation, I created the template named OSCMediaBundle:Form:video_file.html.twig (like the return string of the getName method of the form):

This is not my final template, simply a test:

{% block video_file_widget %}
        {% spaceless %}
            <tr> allo
                <td>
                    {{ form_widget(videoPreset) }}
                </td>
            </tr>
        {% endspaceless %}
    {% endblock %}

Now, in my controller, I have the following:

public function createAction(Request $request)
    {
        $videoFile = new VideoFile();

        $form = $this->createForm('video_file', $videoFile);

        if ($request->getMethod() == 'POST') {
            $form->handleRequest($request);

            if ($form->isValid()) {
                $em = $this->get('doctrine.orm.entity_manager');
                $videoFile = $form->getData();

                $em->persist($videoFile);
                $em->flush();
            }
        }

        return $this->render('OSCMySportBundle:Video:create.html.twig', array(
            'form' => $form->createView()
        ));
    }

In my template OSCMySportBundle:Video:create.html.twig

{{ form(form) }}

Finally, I declared the form in my configuration:

twig:
    debug:            %kernel.debug%
    strict_variables: %kernel.debug%
    globals:
        locales: %locales%
    form:
        resources:
            # ...
            - 'SonataFormatterBundle:Form:formatter.html.twig'
            - 'OSCMediaBundle:Form:video_file.html.twig'

I cleared the cache and everything but I can't get my custom template to show up. I don't know what I'm doing wrong here.

Edit1: After @Zalex's comment, I renamed video_file to video_file_widget in my template. However, I am still not seeing the and in the final html.

Edit2: If I add a text in the template ('allo'), 'allo' is shown but not the and tags, so basically, the template is loaded.

Upvotes: 4

Views: 7265

Answers (1)

zalex
zalex

Reputation: 797

You have to add "widget" in your form template after your custom form name (video_file in your case) :

{% block video_file_widget %}
        {% spaceless %}
            <tr>
                <td>
                    {{ form_widget(form) }}
                </td>
            </tr>
        {% endspaceless %}
    {% endblock %}

Upvotes: 3

Related Questions