I have my own type of form with a name video_file. I'm trying to show my own template for it, but it still shows the default Symfony theme, although I followed every step in the documentation.
Here is my complete configuration:
VideoFileType.php
<?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 where I declare 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 }
I just want to override the form template. As mentioned in the documentation, I created a template with a name OSCMediaBundle:Form:video_file.html.twig(for example, the return line of the form's getName method):
This is not my last template, just 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 that’s all, but I can’t get my custom template. I do not know what I am doing wrong here.
Edit1: @Zalex video_file video_file_widget . , html.
Edit2: ('allo'), "allo", , .