Symfony 2与Twig形式:添加表单变量到现有的类型 [英] Symfony 2 Forms with Twig: add form variable to existing types

查看:169
本文介绍了Symfony 2与Twig形式:添加表单变量到现有的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

我需要我的symfony2 / twig表单来坚持一定的条件: {%block form_row%}
< div class =CONSTANT_CLASS class_based_on_field_type class_based_on_error>
{{form_label(form)}}
{{form_widget(form)}}
...
< / div>
{%endblock form_row%}

请注意,我需要获取字段在form_row块内键入。唉,字段类型只在小部件级别定义。



我绝对需要一种方法让我的form_row知道它正在处理的字段类型。
所以我想最好以某种方式重写 form_row twig函数。



默认的小枝功能被覆盖?怎么可以做到这一点?



请记住,这不是关于自定义表单。我需要知道如何添加到现有的表单变量对于现有的字段类型。



@nifr:
您的关键答案似乎是 {%set typeClass ...%} 。但是模板的任何地方都没有定义变量 text 。查看第158ff行的form_div_layout.html.twig,我认为这个类型只能在form_widget级别设置,因此封装在那里。这意味着使用form_row级别的类型将导致给定的默认值(所以它在测试时发生在我身上)。如果你可以证明这个错误,我会很乐意接受你的答案。

解决方案

在form_row块中,你可以使用:



{{form.vars.block_prefixes [2]}}

form.vars.block_prefixes为您提供了一个包含更多信息的数组它可能会随版本而改变。但从目前我看到的情况来看,总是索引2


I need my symfony2/twig forms to adhere to a certain condition: All form rows must look similar to this:

{% block form_row %}
<div class="CONSTANT_CLASS class_based_on_field_type class_based_on_error">
    {{ form_label(form) }}
    {{ form_widget(form) }}
    ...
</div>
{% endblock form_row %}

Notice that I need to get the field type within the form_row block. Alas, the field type is only defined at the widget level.

I definitely need a way to let my form_row know what type of field it is dealing with. So i suppose it would be best to somehow override the form_row twig function.

Where can the default twig functions be overridden? And how could this be done?

Remember, this is not about customizing a form. I need to know how to add to existing form variables for existing field types.

@nifr: The key answer for you seems to be the {% set typeClass ... %}. But there is not a defined variable text anywhere for the template. Look at form_div_layout.html.twig at line 158ff, I think the type really gets set only at the form_widget level and is thus capsulated to be there. That means using the type at the form_row level will aways result in the given default (So it occurred to me while testing). If you can prove this wrong I will happily accept your answer.

解决方案

In the form_row block you can use :

{{ form.vars.block_prefixes[2] }}

form.vars.block_prefixes gives you an array with more information and it might change with versions. But from what I have seen so far it is always index 2

这篇关于Symfony 2与Twig形式:添加表单变量到现有的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆