【问题标题】:Symfony 3 Form builder : block_name not workingSymfony 3 表单生成器:block_name 不起作用
【发布时间】:2017-10-10 03:22:35
【问题描述】:

symfony documentation 中,据说我们可以重新定义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 的所有东西对我来说都很新。

谢谢

【问题讨论】:

  • 如果你这样做,你将如何处理数据收集?
  • 展示还是自定义?

标签: php symfony twig symfony-3.2


【解决方案1】:

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 文件中形成 _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。我知道树枝模板和表单_self,它正在工作,但我无法自定义de block_name
  • 在 Symfony 2.8 中。 block_name 不能在 attr 内。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-06
相关资源
最近更新 更多