[英]Symfony2 - Creating a template for a custom form type
I have a custom form type named video_file
. 我有一个名为
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. 我正在尝试为它显示一个自定义模板,但它仍然显示默认的Symfony主题,即使我按照文档中的每一步。
Here is my full configuration: 这是我的完整配置:
VideoFileType.php 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): 正如文档中提到的,我创建了名为
OSCMediaBundle:Form:video_file.html.twig
的模板OSCMediaBundle:Form:video_file.html.twig
(类似于OSCMediaBundle:Form:video_file.html.twig
的getName方法的返回字符串):
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
在我的模板
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. Edit1:在@ Zalex的评论之后,我在我的模板中将video_file重命名为video_file_widget。 However, I am still not seeing the and in the final html.
但是,我仍然没有看到和最终的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. Edit2:如果我在模板中添加一个文本('allo'),则显示'allo'但不显示和标签,所以基本上,模板已加载。
You have to add "widget" in your form template after your custom form name (video_file in your case) : 您必须在自定义表单名称后添加“窗口小部件”(在您的情况下为video_file):
{% block video_file_widget %}
{% spaceless %}
<tr>
<td>
{{ form_widget(form) }}
</td>
</tr>
{% endspaceless %}
{% endblock %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.