Symfony 3 | custom data-prototype displayed two times - forms

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.

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

Shopify "New Order Notification" Email Template

I want another item above Total that displays the total quantity.
Any ideas on how to do this with Liquid and have it work within the Shopify Email Template?
Here's my current snippet:
<ul style="list-style-type:none;"> {% for line in line_items %} <li> {{ line.quantity }}x
{{ line.title }} for {{ line.price | money }} each </li> {% endfor %}
<li><br><b>Total: </b>{{ total_price | money }}</li>
</ul>
Any help with this would be most appreciated.
Thanks, Ben
I think you want {{ item_count }}. See the Shopify docs here for a list of the order variables you have access to in email templates:
item_count: A sum of all the items' quantities.

jekyll - make a list of page.tags

I'm new to jekyll and am working on building my site.
I have a "posts" layout where I'd like to have all the tags associated with the post appear in the left column. The problem I'm running into is that using {{ page.tags }} returns a list of tags that are not comma-separated and looks messy. See here for an example.
The html code for the layout page is:
<div class="span3">
</br>
<img src="{{ page.root }}assets/img/glyphicons_045_calendar.png" /> {{ page.date | > date: "%d %B %Y" }}
</br>
<img src="{{ page.root }}assets/img/glyphicons_066_tags.png" /> {{ page.tags }}
</div>
<div class="span9">
<h1> {{ page.title }} </h1>
{{ content }}
</div>
Any advice on how to (a) get the tags list to be comma-separated and (b) wrap around so it stays within the left column? Thanks!
You might try to put them inside a <p> tag so they can wrap around.
To have them comma-separated, you can follow the jekyll docs and use:
{{ page.tags | array_to_sentence_string }} => foo, bar, and baz
As is said in the Jekyll wiki.
For more precise control over what you can do with tags use the for operator:
<ul>
{% for tag in page.tags %}
<li> {{tag}} </li>
{% endfor %}
</ul>
This example is taken verbatim from somewhere. There are also more control structures to choose from.

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

Symfony2 Twig Form Widget Array Access

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