如何从Twig中的Symfony2表单获取Doctrine2实体方法

时间:2021-04-26 06:46:42

I'm in a Twig template, and I have a "form" variable that represents a Doctrine2 Entity Form.

我在Twig模板中,并且我有一个表示Doctrine2实体表单的“表单”变量。

This Entity has properties that are mapped into the form, but the Entity has also some methods that I would like to access from my Twig template.

此实体具有映射到表单的属性,但实体还有一些我想从我的Twig模板访问的方法。

I would love to do something like this:

我很想做这样的事情:

{{ form.myMethod }}

or maybe something like this:

或者类似这样的事情:

{{ form.getEntity.myMethod }}

but unfortunately it doesn't work.

但不幸的是它不起作用。

How could I achieve what I need?

我怎么能达到我的需要呢?

8 个解决方案

#1


34  

To access your entity from your FormView in a twig template you can use the following code

要在树枝模板中从FormView访问您的实体,您可以使用以下代码

{{ form.get('value') }}

Where form is your FormView object. This will return your entity and from there you can call any methods on it. If you embed a collection of entities or a single entity in your form you can access it the same way

表单是FormView对象。这将返回您的实体,然后您可以在其上调用任何方法。如果您在表单中嵌入了一组实体或单个实体,则可以以相同的方式访问它

{{ form.someembedform.get('value') }}

or

要么

{% for obj in form.mycollection %}
  {{ obj.get('value').someMethod }}
{% endif %}

#2


18  

An even more convenient syntax to get the underlying entity instead of

获取底层实体而不是更方便的语法

{{ form.get('value') }}

is this:

这是:

{{ form.vars.value }}

Then you can call any entity method like this:

然后你可以调用这样的任何实体方法:

{{ form.vars.value.someMethod }}

See also the Form chapter in the Symfony2 Docs:

另请参阅Symfony2文档中的表单章节:

http://symfony.com/doc/current/book/forms.html#rendering-a-form-in-a-template

http://symfony.com/doc/current/book/forms.html#rendering-a-form-in-a-template

#3


15  

Just in order to update the subject:

只是为了更新主题:

form.get('value')

is deprecated since symfony 2.1. Copy from Symfony\Component\Form\FormView :

自symfony 2.1以来已弃用。从Symfony \ Component \ Form \ FormView复制:

/*
 * @deprecated Deprecated since version 2.1, to be removed in 2.3. Access
 *             the public property {@link vars} instead.
 */
public function get($name, $default = null) ....

so, I guess

所以,我想

form.vars.value.youMethod()

should be the way to go. It has worked form me.

应该是要走的路。它已经形成了我。

... and there it goes my first post here. hope it helps!

......那里是我的第一篇文章。希望能帮助到你!

#4


5  

Lost few hours trying to figure out what's going on and why

失去了几个小时试图弄清楚发生了什么以及为什么

{{ form.vars.value }}

is NULL.

一片空白。

If you have form.element (not the form object itself) object, for example if you are overriding a form_row template that has passed the form.row object, you can get the Entity like this:

如果你有form.element(不是表单对象本身)对象,例如,如果你覆盖已经传递了form.row对象的form_row模板,你可以得到这样的实体:

{{ form.getParent().vars.value.MyEntityMethod }}

hope that helps someone!

希望有人帮助!

EDIT: Year and so later - another useful tip:

编辑:一年等等 - 另一个有用的提示:

{% block sonata_type_collection_widget %}
    {% for child in form %}
        {{ child.vars.form.vars.value.name }}
    {% endfor %}
{% endblock %}

#5


3  

object methods should work in twig, I know I used them in some project.

对象方法应该在twig中工作,我知道我在一些项目中使用它们。

try to use ()

尝试使用()

like {{ form.myMethod() }}

像{{form.myMethod()}}

#6


1  

It seems that at some point the value is actually null. So you can use

似乎在某些时候该值实际上是空的。所以你可以使用

{{ (form.vars.value != null) ? form.vars.value.yourEntityMethod():'' }}

tested in SF v3.0.6.

在SF v3.0.6中测试。

#7


0  

None of the above worked for me in version 2.6.7. I used customised form widgets to achieve this:

在2.6.7版中,以上都不适用于我。我使用自定义表单小部件来实现此目的:

{# src/AppBundle/Resources/views/Form/fields.html.twig #}
{% extends 'form_div_layout.html.twig' %}

{%- block entity_widget -%}
    <div {{ block('widget_container_attributes') }}>
    {%- for n, child in form %}
        {{- form_widget(child, {
            'entity': form.vars.choices[n].data
        }) -}}
        {{- form_label(child) -}}
    {% endfor -%}
    </div>
{%- endblock %-}

{%- block radio_widget -%}
{# You now have access to entity #}
{%- endblock %-}

#8


-1  

use {{ form.getData.myMethod }}.

使用{{form.getData.myMethod}}。

#1


34  

To access your entity from your FormView in a twig template you can use the following code

要在树枝模板中从FormView访问您的实体,您可以使用以下代码

{{ form.get('value') }}

Where form is your FormView object. This will return your entity and from there you can call any methods on it. If you embed a collection of entities or a single entity in your form you can access it the same way

表单是FormView对象。这将返回您的实体,然后您可以在其上调用任何方法。如果您在表单中嵌入了一组实体或单个实体,则可以以相同的方式访问它

{{ form.someembedform.get('value') }}

or

要么

{% for obj in form.mycollection %}
  {{ obj.get('value').someMethod }}
{% endif %}

#2


18  

An even more convenient syntax to get the underlying entity instead of

获取底层实体而不是更方便的语法

{{ form.get('value') }}

is this:

这是:

{{ form.vars.value }}

Then you can call any entity method like this:

然后你可以调用这样的任何实体方法:

{{ form.vars.value.someMethod }}

See also the Form chapter in the Symfony2 Docs:

另请参阅Symfony2文档中的表单章节:

http://symfony.com/doc/current/book/forms.html#rendering-a-form-in-a-template

http://symfony.com/doc/current/book/forms.html#rendering-a-form-in-a-template

#3


15  

Just in order to update the subject:

只是为了更新主题:

form.get('value')

is deprecated since symfony 2.1. Copy from Symfony\Component\Form\FormView :

自symfony 2.1以来已弃用。从Symfony \ Component \ Form \ FormView复制:

/*
 * @deprecated Deprecated since version 2.1, to be removed in 2.3. Access
 *             the public property {@link vars} instead.
 */
public function get($name, $default = null) ....

so, I guess

所以,我想

form.vars.value.youMethod()

should be the way to go. It has worked form me.

应该是要走的路。它已经形成了我。

... and there it goes my first post here. hope it helps!

......那里是我的第一篇文章。希望能帮助到你!

#4


5  

Lost few hours trying to figure out what's going on and why

失去了几个小时试图弄清楚发生了什么以及为什么

{{ form.vars.value }}

is NULL.

一片空白。

If you have form.element (not the form object itself) object, for example if you are overriding a form_row template that has passed the form.row object, you can get the Entity like this:

如果你有form.element(不是表单对象本身)对象,例如,如果你覆盖已经传递了form.row对象的form_row模板,你可以得到这样的实体:

{{ form.getParent().vars.value.MyEntityMethod }}

hope that helps someone!

希望有人帮助!

EDIT: Year and so later - another useful tip:

编辑:一年等等 - 另一个有用的提示:

{% block sonata_type_collection_widget %}
    {% for child in form %}
        {{ child.vars.form.vars.value.name }}
    {% endfor %}
{% endblock %}

#5


3  

object methods should work in twig, I know I used them in some project.

对象方法应该在twig中工作,我知道我在一些项目中使用它们。

try to use ()

尝试使用()

like {{ form.myMethod() }}

像{{form.myMethod()}}

#6


1  

It seems that at some point the value is actually null. So you can use

似乎在某些时候该值实际上是空的。所以你可以使用

{{ (form.vars.value != null) ? form.vars.value.yourEntityMethod():'' }}

tested in SF v3.0.6.

在SF v3.0.6中测试。

#7


0  

None of the above worked for me in version 2.6.7. I used customised form widgets to achieve this:

在2.6.7版中,以上都不适用于我。我使用自定义表单小部件来实现此目的:

{# src/AppBundle/Resources/views/Form/fields.html.twig #}
{% extends 'form_div_layout.html.twig' %}

{%- block entity_widget -%}
    <div {{ block('widget_container_attributes') }}>
    {%- for n, child in form %}
        {{- form_widget(child, {
            'entity': form.vars.choices[n].data
        }) -}}
        {{- form_label(child) -}}
    {% endfor -%}
    </div>
{%- endblock %-}

{%- block radio_widget -%}
{# You now have access to entity #}
{%- endblock %-}

#8


-1  

use {{ form.getData.myMethod }}.

使用{{form.getData.myMethod}}。