Symfony 3表单构建器:block_name不起作用

3
Symfony文档 中,提到我们可以重新定义block_name来实现更好的自定义,但是看起来并没有生效。
这是我尝试过的方法:

在声明集合时:

$builder
    ->add('medias', CollectionType::class, array(
        'entry_type' => MediaType::class,
        'block_name' => 'media_proto'
    ));

在每个集合的字段上

//MediaType.php
$builder
    ->add('detail', TextType::class, array(
        'translation_domain' => 'messages',
        'label' => 'person.medias.detail',
        'block_name' => 'media_proto'
    ))
    ->add('typeMedia', EntityType::class, array(
        'class' => 'VSCrmBundle:TypeMedia',
        'choice_translation_domain' => true,
        'translation_domain' => 'messages',
        'label' => 'person.medias.type',
        'block_name' => 'media_proto'
    ))

在集合的configureOptions中

//MediaType.php
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'VS\CrmBundle\Entity\Media',
        'block_name' => 'media_proto'
    ));
}

生成的HTML始终相同。
<input type="text" id="user_parent_person_medias_0_detail" name="user_parent[person][medias][0][detail]" required="required" class="" value="">

我的目的是统一这些块的名称,这样我就可以全局自定义它们。

也许我有什么误解,因为Symfony的所有东西对我来说都是相当新的。

谢谢。


如果你打算这样做,那么你将如何处理数据集合? - Herr Nentu'
展示它还是自定义它? - Robouste
1个回答

0

block_name不是texttype字段继承选项的一部分。为了渲染它,您应该使用

添加它。
$builder
    ->add('detail', TextType::class, array(
        'translation_domain' => 'messages',
        'label' => 'person.medias.detail',
        'attr'=>array('block_name' => 'media_proto')
    ))
    ->add('typeMedia', EntityType::class, array(
        'class' => 'VSCrmBundle:TypeMedia',
        'choice_translation_domain' => true,
        'translation_domain' => 'messages',
        'label' => 'person.medias.type',
        'attr'=>array('block_name' => 'media_proto')
    ))

试一下!

顺便说一下,你想要使用链接连接到Symfony文档中的特定链接需要在twig文件中添加twig模板和form _self代码部分才能使其正常工作。

编辑

看起来这个方法不再起作用了。我记得这个方法适用于sf2。

你需要做的是确定你需要覆盖的字段,然后在twig中添加你需要覆盖的块。假设你的字段是:选择和文本类型,并且是从form_div_layout.html.twig中获取的:

{% form_theme name_of_your_form _self %}

{%- block form_widget_simple -%}
    {%- set type = type|default('text') -%}
    <input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %} block_name="media_proto"/> <!-- code you need -->
{%- endblock form_widget_simple -%}


{%- block choice_widget_collapsed -%}
    {%- if required and placeholder is none and not placeholder_in_choices and not multiple and (attr.size is not defined or attr.size <= 1) -%}
        {% set required = false %}
    {%- endif -%}
    <select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %} block_name="media_proto">
        {%- if placeholder is not none -%}
            <option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ placeholder != '' ? (translation_domain is same as(false) ? placeholder : placeholder|trans({}, translation_domain)) }}</option>
        {%- endif -%}
        {%- if preferred_choices|length > 0 -%}
            {% set options = preferred_choices %}
            {{- block('choice_widget_options') -}}
            {%- if choices|length > 0 and separator is not none -%}
                <option disabled="disabled">{{ separator }}</option>
            {%- endif -%}
        {%- endif -%}
        {%- set options = choices -%}
        {{- block('choice_widget_options') -}}
    </select>
{%- endblock choice_widget_collapsed -%}

我不确定这是否是最佳解决方案,但希望能有所帮助!


嗨,这只是添加了一个属性“block_name”,但它并没有改变block_name。我知道对于twig模板和form _self,它是有效的,但我无法自定义块名称。 - Robouste
1
在Symfony 2.8中,block_name不能放在attr内部。 - Samuel Vicent

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接