【问题标题】:Customize Form doesn't works自定义表格不起作用
【发布时间】:2018-06-22 18:55:02
【问题描述】:

我正在尝试在 Symfony 3.3.2 (https://symfony.com/doc/2.8/form/form_customization.html) 中自定义表单渲染。

app/Resources/views/form/fields.html.twig

{% block test %}
  <p>TEST</p>
{% endblock %}

app/Resources/views/default/new.html.twig

{% form_theme form 'form/fields.html.twig' %}

{{ form_widget(form.age) }}
{{ block('test') }}

src/AppBundle/Controller/DefaultController.php

class DefaultController extends Controller
{
    public function indexAction(Request $request)
    {
      $test = new Test();
      $test->setAge(8);

      $form = $this->createForm(TestType::class, $test);

      return $this->render('default/new.html.twig', array(
      'form' => $form->createView(),
      ));
    }
}

src/AppBundle/Entity/Test.php

namespace AppBundle\Entity;

class Test
{
    protected $age;

    public function getAge()
    {
        return $this->age;
    }

    public function setAge($age)
    {
        $this->age = $age;
    }
}

src/AppBundle/Form/TestType.php

class TestType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('age')
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => Test::class,
        ));
    }
}

app/config/config.yml

twig:
    debug: '%kernel.debug%'
    strict_variables: '%kernel.debug%'
    form_themes:
      - 'form/fields.html.twig'

我想我误解了一些东西,因为我希望看到一个带有 8 数字的输入,并且在字符串“TEXT”下方,但实际上,我只有输入。那么如何在 new.html.twig 中插入自定义块呢?

【问题讨论】:

    标签: php symfony


    【解决方案1】:

    form_theme 不是一种包含其他块的方法,它只是一种带有一些约定的样式。如果您需要为表单赋予样式,则只需使用主题和form_widget。 如果您需要包含其他块,请创建该块(在单独的文件中,甚至在同一个文件中)并在 embedinclude 指令之间进行选择。 对于embedinclude 之间的区别,我建议阅读this SO answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多