Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
750 views
in Technique[技术] by (71.8m points)

symfony - Directly access a form field's value when overriding widget in a twig template

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 %}

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

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 }}

which didn't work for me.

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

    object(SymfonyComponentFormFormView)[331]
    private 'vars' => 
      array (size=15)
        'value' => 
          object(PanasonicTestEtAvisBundleEntityProduct)[168]
            protected 'reviewArray' => 
              object(DoctrineORMPersistentCollection)[234]
                ...
            protected 'testArray' => 
              object(DoctrineORMPersistentCollection)[221]
                ...
            protected 'fbshareArray' => 
              object(DoctrineORMPersistentCollection)[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(SymfonyComponentFormFormView)[331]
        'id' => string 'panasonic_testetavisbundle_producttype' (length=38)
        'name' => string 'panasonic_testetavisbundle_producttype' (length=38)
        'full_name' => string 'panasonic_testetavisbundle_producttype' (length=38)

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.

Any ideas?

Edit2: I found out the real problem...

Edit3: 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

{{ form.vars.value.title }}

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

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 }}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...