Symfony2 Twig Form Widget Array Access - forms

I have a form widget with several choices (many-to-many relationship)
in twig template I can iterate over the checkboxes:
{% for choice in form.downloads %}
{{ form_widget(choice) }} {{ form_label(choice) }}<br />
{% endfor %}
I'd like to acces the choices directly (they should bi formatted end positioned differently)
I tried several syntaxes but the doesn't work
{{ form_widget(form.downloads.0) }} {{ form_label(form.downloads.0) }}<br />
{{ form_widget(form.downloads['0']) }} {{ form_label(form.downloads['0']) }}<br />
{{ form_widget(form.downloads[0]) }} {{ form_label(form.downloads[0]) }}<br />
Do I use the wrong array keys or is array access generally not possible?

Array Access is possible when you're using Twig.
I guess the error you got when you're trying to access the first generated checkbox using
{{ form_widget(form.downloads.0) }} {{ form_label(form.downloads.0) }}<br />
is
Method "0" for object "Symfony\Component\Form\FormView" does not exist in ...
So, you've just to use the child name of your checkbox. You should have in your buildForm something like:
$builder->add('childName', 'anyTypeYouWant', array())
But I guess you're using collection type to generate your checkboxes. In this specific case
{{ form_widget(form.downloads.0) }} {{ form_label(form.downloads.0) }}<br />
should work fine! I already used it to access specific collection fields without customized keys.
You should also use the twig debug extension to check your form.downloads
{% debug form.downloads %}
and if the debug doesn't work, you've to add in your "app/config/config.yml" file
services:
debug.twig.extension:
class: Twig_Extensions_Extension_Debug
tags: [{ name: 'twig.extension' }]

Related

How to customize one specific field in Symfony Forms

I am rendering a form that contains several choice fields. I want to render one specific choice field differently from the other ones.
I've tried to use:
{% form_theme form 'MyBundle:Form:my-choice-field.html.twig' %}
{{ form_widget(field) }}
But that changes the look and feel of all choice fields in that form. If I place form_theme right next to the field I want to render differently, it does nothing.
I have thought of creating a new Form Type but it seems overkilling, I just want a different style for that specific field. Any ideas?
In the doc there is a section describing how to personalize an individual field, for example to customize the name field of a product entity only:
{% form_theme form _self %}
{% block _product_name_widget %}
<div class="text_widget">
{{ block('form_widget_simple') }}
</div>
{% endblock %}
{{ form_widget(form.name) }}
Here, the _product_name_widget fragment defines the template to use for the field whose id is product_name (and name is product[name]).
You can also override the markup for an entire field row using the same method:
{% form_theme form _self %}
{% block _product_name_row %}
<div class="name_row">
{{ form_label(form) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
</div>
{% endblock %}
{{ form_row(form.name) }}

Symfony 3 | custom data-prototype displayed two times

I try to display my custom data-prototype for a CollectionType. I followed this answer.
This is what I have:
{{ form_start(form, {'attr': {'class': 'form-horizontal'}}) }}
<div class="form-group">
{{ form_label(form.answers, "Answers", {'label_attr': {'class': 'col-sm-2 control-label'}}) }}
{{ form_errors(form.answers) }}
<div id="mybundle_add_answers"
data-prototype="
{% filter escape %}
{% include 'MyBundle:Form:prototype_answers.html.twig' with {'form': form.answers.vars.prototype} %}
{% endfilter %}">
</div>
Add
</div>
{{ form_rest(form) }}
{{ form_end(form) }}
This is working but, because of form_rest, it displays my data-prototype (mybundle_add_answers) a second time at the end of the form.
It seems that nobody have this problem so What did I do wrong here / forget? I use Symfony 3.0 maybe this is related?
I didn't find a way to do what I wanted with custom data-prototype. I don't know if this is because of Symfony 3 that I have a double widget.
Anyway I did what I wanted with custom collection_widget. If anyone is interested in : https://gist.github.com/cowlby/1294186
And if someone find an answer for this "double" widget problem, I'm still curious to know.
I've had the exact same problem.
After spending a day searching the web for this problem, I ended up simply not using {{ form_rest(form) }} and {{ form_end(form) }}. In my template I added the csrf token manually by using {{ form_widget(form._token) }}, then closed the </form> manually, because in my case that's all form_rest and form_end did.

Symfony submitting individual forms on page with multiple forms

My Symfony project consists of 3 bundles 2 of which provides forms.
I use twig in my view templates.
The first bundle has a view which includes the forms from the other 2 bundles.
My problem is when I attempt to submit either of the 2 included forms, the other form's required field is highlighted - which interferes with either of the forms submissions and data is not committed.
How I can have multiple forms on the same page which don't interfere with each others submission?
Make sure you are not nesting the forms.
Something like this will be fine:
{{ form_start(myForm) }}
{{ form_widget(myForm) }}
{{ form_end(myForm) }}
{{ form_start(anotherForm) }}
{{ form_widget(anotherForm) }}
{{ form_end(anotherForm) }}
but something like this will not:
{{ form_start(myForm) }}
{{ form_widget(myForm) }}
{{ form_start(anotherForm) }}
{{ form_widget(anotherForm) }}
{{ form_end(anotherForm) }}
{{ form_end(myForm) }}

Errors for form in rendered template

I have this :
template edit.html.twig
{% extends "....:...:layout.html.twig" %}
{% block body %}
{{ render(controller('EVeilleurFlowerBundle:Catalogue:add' )) }}
{{ render(controller('EVeilleurFlowerBundle:Catalogue:delete' )) }}
{% endblock %}
And the template "add" is basically a single form.
When the user enters "wrong" data in the add form, it automatically redirects to add.html.twig, displaying a single form ( wtih the error ) without layout, css etc...
Can i change the redirection, or am I building my templates the wrong way ? (pretty much newbie here :) )
Thanks

Pyro Group Validation

Background
I want to display a link to the admin area if a user is part of the Administrators group
so in my view code I want to do something like;
{{ if user:group group_name="administrators" }}
Admin Area
{{ endif }}
I found the solution after much messing around but I got it to work;
{{ if user:group=="admin" }}
.....html code
{{ endif }}