gpt4 book ai didi

symfony - 覆盖 Twig 模板中的小部件时直接访问表单字段的值

转载 作者:行者123 更新时间:2023-12-03 06:01:24 25 4
gpt4 key购买 nike

我想要做的是获取存储在表单 View 中的变量。

{% form_theme edit_form _self %}

{% block field_widget %}
{% spaceless %}
{% set type = type|default('text') %}
<input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>

{# MY CODE #}
{% if type == "file" %}
<a class="BOpreview" href="{# NEED TO REPLACE VAR HERE #}">Aperçu</a>
{% endif %}

{# MY ATTEMPT #}
{{ form.title.get('value') }}
{{ form.vars.value.url }}

{% endspaceless %}
{% endblock field_widget %}

我的表单具有 url、标题等属性,我尝试在此处访问它们以在字段小部件 block 中使用它们。

我搜索了它并找到了https://groups.google.com/forum/?fromgroups=#!topic/symfony2/onor9uFte9E建议:

{{ form.title.get('value') }}
{{ form.vars.value.url }}

这对我不起作用。

注意:如果我在 Controller 中的 $form->createView() 上执行 var_dump,我会得到:

    object(Symfony\Component\Form\FormView)[331]
private 'vars' =>
array (size=15)
'value' =>
object(Panasonic\TestEtAvisBundle\Entity\Product)[168]
protected 'reviewArray' =>
object(Doctrine\ORM\PersistentCollection)[234]
...
protected 'testArray' =>
object(Doctrine\ORM\PersistentCollection)[221]
...
protected 'fbshareArray' =>
object(Doctrine\ORM\PersistentCollection)[317]
...
private 'id' => int 2
private 'name' => string 'Nom du produit' (length=14)
private 'title' => string '<span>Titre </span>' (length=19)
private 'image' => string 'bundles/testetavis/uploads/product/0d9d9550.png' (length=47)
private 'fbImage' => string 'bundles/testetavis/uploads/product/facebook//product_e928cd96.jpg' (length=65)
private 'description' => string '<span>Descriptif </span>' (length=24)
private 'url' => string 'http://www.google.com' (length=21)
private 'creationDate' =>
object(DateTime)[210]
...
private 'modificationDate' =>
object(DateTime)[209]
...
private 'isDeleted' => int 0
'attr' =>
array (size=0)
empty
'form' =>
&object(Symfony\Component\Form\FormView)[331]
'id' => string 'panasonic_testetavisbundle_producttype' (length=38)
'name' => string 'panasonic_testetavisbundle_producttype' (length=38)
'full_name' => string 'panasonic_testetavisbundle_producttype' (length=38)

例如,我想访问该网址,但在经过多次变化后似乎无法做到这一点。包括使用 {{ value }}、{{ value.url }}但尽管存在变量,我仍然可以执行 {{ full_name }} 并获取 panasonic_testetavisbundle_producttype。

有什么想法吗?

编辑2:我发现了真正的问题......

Edit3:看到这个问题很受欢迎,我决定澄清一下我试图做的事情,以防它对处于相同情况的人有所帮助。如果你严格依赖问题所问的内容,正如我从我的研究中所说的那样,贝斯尼克支持的确实是正确的。现在我想要做的是对于每个输入类型文件,从用于呈现表单的对象获取 url,并使用检索到的 url 在输入类型文件旁边附加一个预览链接。如果您尝试在我的代码中获取输入类型"file"的表单变量,例如“{{ form.vars.value.url }}”,则这不起作用,因为如果我没记错的话,您会收到一个 token 而不是存储在对象内部的 url。

最佳答案

您可以通过 form.vars.value 访问表单的当前数据:

{{ form.vars.value.title }}

请参阅 Symfony2 Forms 文档:http://symfony.com/doc/current/book/forms.html#rendering-a-form-in-a-template

使用dump函数转储变量:

{{ dump(form.vars.value) }}

如果您正在使用子表单或想要具有特定字段的值:

{{ form.FIELD.vars.VALUE }}

关于symfony - 覆盖 Twig 模板中的小部件时直接访问表单字段的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12497133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com