覆盖树枝模板中的小部件时直接访问表单字段的值 [英] Directly access a form field's value when overriding widget in a twig template

查看:23
本文介绍了覆盖树枝模板中的小部件时直接访问表单字段的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是获取存储在表单视图中的变量.

What I want to do is get variables stored in form 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、标题等属性,我试图在此处访问它们以在字段小部件块中使用它.

My form has properties like url, title, etc and I am trying to access them here to use it in the field widget block.

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

I searched for it and came on https://groups.google.com/forum/?fromgroups=#!topic/symfony2/onor9uFte9E that suggested:

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

这对我不起作用.

注意:如果我在控制器中对 $form->createView() 执行 var_dump,我会得到:

Note: If I do a var_dump on $form->createView() in my controller, I get:

    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.

I want to access that url for instance but can't seem to be able to do it after many variations. Including use of {{ value }}, {{ value.url }} But inspite of vars, I can do {{ full_name }} and get panasonic_testetavisbundle_producttype.

有什么想法吗?

Edit2:我发现了真正的问题......

I found out the real problem...

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

Seeing that this question is quite popular I decided to clarify on what I attempted to do in case it helps someone in the same situation. If you rely strictly on what the question asks, as I stated from my research and that Besnik supported are indeed correct. Now what I wanted to do is for every input type file, get url from object used to render form and append a preview link, using retrieved url, beside the input type file. If you try to get the form var of an input type "file" like this "{{ form.vars.value.url }}" in my code, this doesn't work since, if I recall correctly, you receive a token instead of the url stored inside the object.

推荐答案

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

You can access the current data of your form via form.vars.value:

{{ form.vars.value.title }}

参见 Symfony2 表单文档:http://symfony.com/doc/current/book/forms.html#rendering-a-form-in-a-template

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

使用 dump 函数转储变量:

Dump vars by using dump function:

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

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

If you are using subforms or want to have a value of a specific field:

{{ form.FIELD.vars.VALUE }}

这篇关于覆盖树枝模板中的小部件时直接访问表单字段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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