Php Symfony 3表单生成器:块名称不工作
在中,据说我们可以重新定义Php Symfony 3表单生成器:块名称不工作,php,symfony,twig,symfony-3.2,Php,Symfony,Twig,Symfony 3.2,在中,据说我们可以重新定义block\u name以实现更好的定制,但它似乎不起作用 以下是我尝试过的: 声明集合时 在每个集合的字段上 集合的内部配置选项 生成的html总是相同的 <input type="text" id="user_parent_person_medias_0_detail" name="user_parent[person][medias][0][detail]" required="required" class="" value=""> 我的目的是
block\u name
以实现更好的定制,但它似乎不起作用
以下是我尝试过的:
声明集合时
在每个集合的字段上
集合的内部配置选项
生成的html总是相同的
<input type="text" id="user_parent_person_medias_0_detail" name="user_parent[person][medias][0][detail]" required="required" class="" value="">
我的目的是让这些区块名成为制服,这样我就可以在全球范围内定制它们
也许我误解了什么,因为Symfony的东西对我来说都是全新的
谢谢block_name不是texttype字段的Inherited选项的一部分。为了渲染它,您应该使用
$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文档的特定链接做什么,需要细枝模板的一部分和细枝文件中的form_self代码,才能让它以这种方式工作
已编辑
看来这已经不起作用了。我记得这对sf2有效
您需要做的是确定需要覆盖的字段,然后在twig中添加需要覆盖的块。让我们想象一下,您的字段是:choice和text类型,并取自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 -%}
{%form\u主题名称\u您的\u表单\u self%}
{%-block form_widget_simple-%}
{%-set type=type | default('text')-%}
{%-endblock form_widget_simple-%}
{%-block choice_widget_collapsed-%}
{%-如果需要,且占位符在_选项中为无且不是占位符_,且不是多个且(未定义attr.size或attr.size 0-%}
{%set options=首选选项%}
{{-block('choice_widget_options')-}
{%-如果选项|长度>0且分隔符不是none-%}
{{分隔符}}
{%-endif-%}
{%-endif-%}
{%-设置选项=选项-%}
{{-block('choice_widget_options')-}
{%-endblock choice_widget_collapsed-%}
我不知道这是否是最好的解决方案,但希望它能有所帮助!如果你能做到这一点,你将如何处理数据收集?显示还是自定义?嗨,这只是添加了一个属性“block\u name”,但它不会更改block\u name。我知道细枝模板和表单本身,它正在工作,但我无法自定义de block\u nameIn Symfony 2.8.块名称不能在属性内。
<input type="text" id="user_parent_person_medias_0_detail" name="user_parent[person][medias][0][detail]" required="required" class="" value="">
$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')
))
{% 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 -%}