How to use form field value in twig template? - symfony

i have a formular and in there I have a collection of entities. In my template i would like to use this:
{% for service in form.services %}
{{ form_label(service) }}
{{ form_errors(service) }}
{{ form_widget(service, {'attr': {'class': service.name}}) }}
{% endfor %}
Is it posiible to fetch a field name from the collection like in my example the field name?
With this i got an error.
Thanks Stefan

In the debug toolbar, click on the form icon. There you can see what your form variable contains. services is probably an array of subforms. You can access a specific subform and then get the value of the "name" property like this: services[0].name.vars.value or you can of course loop through the whole array.

Related

compare a input field value to a string or text symfony2

I was trying to learn about framework and Ill be starting under "Symfony", then i got this problem, I create a
{{ form_widget(form.type) }}
equivalent to
<input type="text" class="type" id="type"/>
this element "input" have a value that given by a tab or menu if i click on it, example jumping.how do i compare it to a text using "Symfony" if statement like the logic below.
{% if jumping == "text" %}
//it will do something
{% endif %}
as explained nicely in the symfony docs here you need to use the form.vars.value to get the input field's value
so for someting like {{ form_widget(form.name) }} you would access the values by doing {{ form.vars.value.name }}
in your case it would be
{% if form.vars.value.name == "text" %}
//do something
{% endif %}

Symfony2 - get value displayed to user for choice field in twig

I am trying to display the "label" (ie, value displayed to user) of a choice field in my twig template. I'm using Symfony 2.3. I just need the label associated with the actual current value of the field.
I finally figured out a way to do it that requires looping through every choice option, but am now wondering if there is a more efficient way? Here is my working code:
{% for choice in form.myfield.vars.choices %}
{% if choice.value == form.myfield.vars.value %}
{{ choice.label }}
{% endif %}
{% endfor %}
I'm looking for something like this (non-working code):
{{ form.myfield.vars.choices[form.myfield.vars.value].label }}
Is there a more efficient way, or is my solution the best possible?

Symfony2 - access form from a widget block template

Following is how the form is rendered.
<fieldset class="properties">
{% block form_content %}
{{ form_widget(form) }}
{% endblock %}
</fieldset>
Now I can access any form field in this template, like {{ form.description }}, it's all good . But here I have a collection field in this form, let's call it collection, I have built a custom field type for this, the block template for this custom type is customCollect_widget, everything until this point is fine, but if I want to access the collection object in this widget template, I got an error saying the field name does not exist in the form object.
Here's my widget template:
{% block customCollect_widget %}
{% spaceless %}
{% for aa in form.collections %}
<div>something</div>
{% endfor %}
....
<% endblock %}
The problem, as I figured, is that the form isn't the same object that's passed to the code above. Is there any workaround to it?
Ha, I solved it. In the collection type widget template, the form variable is referencing the form field type itself, in this case, the collection type. So to loop through the collection in the widget block, you just do {% for child in form %}.

Show image name in Twig, relation one to one

I have a one to one relation between Player and Image. I have also this line in my controller:
var_dump($players[0]->getLinkedImage1()->getName());
It shows the name of an image correctly.
And I have also this line in the template:
{% for players in player %}
{{ player.age }}
{{ player.linkedImage1.name }}
{% endfor %}
but I get this error:
Impossible to access an attribute ("name") on a NULL variable ("")
I expected the last line shown the same name as in the controller.
EDIT: finally I found out that the property was public, that was the reason. Anway I still understand it..
My bad !
Look your for loop..
Try
{% for player in players %}
How did you write the property ? linkedImage1?
If you wrote something like linked_image_1 or linkedImage_1 you should call
{{ player.linked_image_1 }}
or
{{ player.linkedImage_1 }}
then twig will call the related getter according to : http://api.symfony.com/2.4/Symfony/Component/DependencyInjection/Container.html#method_camelize
If its a virtual getter you can directly access with :
{{ player.getLinkedImage1().name }}
or {{ player.getLinkedImage1().getName() }} `
You should try
{{ player.getLinkedImage1().getName() }}

Get number of choices from entity field in Twig template

I have a Symfony 2 form for a Doctrine entity with an entity choice field for a ManyToMany relationship. It is possible for the choice field to not have any choices - how can I test for this in Twig?
Example: The form is for a "Deal" entity, which can optionally be assigned to a "Location" entity. I render the "Location" entity field like this:
{{ form_label(edit_form.locations) }}
{{ form_errors(edit_form.locations) }}
{{ form_widget(edit_form.locations) }}
The field is set to render with checkboxes. However, it is possible that no "Location" records will exist. In this scenario the user will see the label "Locations: " but no check boxes. I would like to have a conditional statement that displays a message, something along the lines of this:
{{ form_label(edit_form.locations) }}
{{ form_errors(edit_form.locations) }}
{{ form_widget(edit_form.locations) }}
{% if edit_form.locations.choices|length == 0 %}
You haven't created any Locations yet!
{% endif %}
Anybody know of a way to achieve this?
Just to help others...
In newer versions it would be:
{% if edit_form.locations.vars.choices|length == 0%}
I was actually looking to do this, so I'll share:
If you had an embedded form and you wanted to do the same type of check except pulling the data from the prototype (in case the form is not generated with any children forms)
{% if form.childForms.vars.prototype.field.vars.choices|length == 0 %}
"childForms" being the embedded form type and "field" being the select field you want the choices out of...
Found it:
{% if edit_form.locations.get('choices')|length == 0 %}

Resources