![](/img/trans.png)
[英]Creating a custom form type in symfony2: can't overwrite buildView()
[英]Symfony2 - Creating a template for a custom form type
我有一個名為video_file
的自定義表單類型。 我正在嘗試為它顯示一個自定義模板,但它仍然顯示默認的Symfony主題,即使我按照文檔中的每一步。
這是我的完整配置:
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';
}
}
這是我將表單聲明為服務的地方:
osc_media.form.type.video_file:
class: %osc_media.form.type.video_file.class%
arguments: [@service_container]
tags:
- { name: form.type, alias: video_file }
簡單地說,我想要的是覆蓋表單模板。 正如文檔中提到的,我創建了名為OSCMediaBundle:Form:video_file.html.twig
的模板OSCMediaBundle:Form:video_file.html.twig
(類似於OSCMediaBundle:Form:video_file.html.twig
的getName方法的返回字符串):
這不是我的最終模板,只是一個測試:
{% block video_file_widget %}
{% spaceless %}
<tr> allo
<td>
{{ form_widget(videoPreset) }}
</td>
</tr>
{% endspaceless %}
{% endblock %}
現在,在我的控制器中,我有以下內容:
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()
));
}
在我的模板OSCMySportBundle:Video:create.html.twig
{{ form(form) }}
最后,我在配置中聲明了表單:
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
globals:
locales: %locales%
form:
resources:
# ...
- 'SonataFormatterBundle:Form:formatter.html.twig'
- 'OSCMediaBundle:Form:video_file.html.twig'
我清除了緩存和一切,但我無法顯示自定義模板。 我不知道我在這里做錯了什么。
Edit1:在@ Zalex的評論之后,我在我的模板中將video_file重命名為video_file_widget。 但是,我仍然沒有看到和最終的HTML。
Edit2:如果我在模板中添加一個文本('allo'),則顯示'allo'但不顯示和標簽,所以基本上,模板已加載。
您必須在自定義表單名稱后添加“窗口小部件”(在您的情況下為video_file):
{% block video_file_widget %}
{% spaceless %}
<tr>
<td>
{{ form_widget(form) }}
</td>
</tr>
{% endspaceless %}
{% endblock %}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.