Error in nested form and twig display - php

I have a problem with errors display in my twig templates.
Here is my twig with one nested form form.pictures :
{{ form_start(form) }}
{% if not form.vars.valid %}
<div class="flash-errors-wrapper">
{{ form_errors(form) }}
<div class="form-errors">{{ form_errors(form.pictures) }}</div>
</div>
{% endif %}
{% for formChild in form.pictures %}
<div class="child">
{% if not formChild.vars.valid %}
<div class="flash-errors-wrapper">
{{ form_errors(form) }}
<div class="form-errors">{{ form_errors(form.picture) }}</div>
<div class="form-errors">{{ form_errors(form.caption) }}</div>
</div>
{% endif %}
{{ form_widget(formChild.picture) }}
{{ form_widget(formChild.caption) }}
</div>
{% endfor %}
{{ form_end(form) }}
After submission, when a child form is non valid, my parent form is not valid too.
Problem is that it display empty div on top like :
<div class="flash-errors-wrapper">
<div class="form-errors"></div>
</div>
I don't want that because css exist on flash-errors-wrapper class so style is applied.
Any ideas?

You certainly need to check if one child form is not valid when you check if the parent form is valid. One way to do it would be (Untested, it may need some adaptation):
{% if not form.vars.valid %}
{% set all_childs_valid = True %}
{% for formChild in form.pictures %}
{% if not formChild.vars.valid %}
{% set all_childs_valid = False %}
{% endif %}
{% endfor %}
{% if all_childs_valid %}
<div class="flash-errors-wrapper">
{{ form_errors(form) }}
<div class="form-errors">{{ form_errors(form.pictures) }}</div>
</div>
{% endif %}
{% endif %}

I usually display form errors just as described in docs:
{% if errors|length > 0 %}
<ul>
{% for error in errors %}
<li>{{ error.message }}</li>
{% endfor %}
</ul>
{% endif %}

Related

get first item in loop

I am looping through an array and would like to add a class if it is the first item.
I have tried
{% if field|first %}
{% if field.first %}
{% if field.first() %}
{% if loop|first %}
{% if loop.first %}
{% if loop.first() %}
none work, please help. (the reason I don't use css pseudo first is that this is a grid layout and I have am doing responsive layout things with it)
{% for field in row.fields %}
<div class="c-product-table__item {% if field|first %}{{ first_item }}{% endif %} ">
<span class="c-product-table__text">{{ field.text }}</span>
</div>
{% endfor %}
I don't know why twig filters are not working, I solved it another way. I set a var and made it blank after first loop
{% set thiscount = 'first_item' %}
{% for field in row.fields %}
<div class="c-product-table__item {{ thiscount }}">
<span class="c-product-table__text">{{ field.text }}</span>
</div>
{% set thiscount = '' %}
{% endfor %}

Twig show n keys of array or all

I have an editor with a set of buttons, and I want to display only a set of buttons based on twig::render variables.
If I include all I want it to display are buttons available, if I include individual button keys I want to display only that ones.
echo TwigLoader::render('#ui/editor.html.twig'['toolbar'=>['all']]);
echo TwigLoader::render('#ui/editor.html.twig'['toolbar'=>['font','size']]);
For the template I'm using the following code:
{% set toolbar_tools = [
{'font':'<select class="ql-font"></select>'},
{'size':'<select class="ql-size"></select>'}]
%}
<div id="button-container">
<span class="ql-formats">
{% for tool, key in toolbar_tools %}
{{ tool.key|raw}}
{% endfor %}
</span>
</div>
I'm getting an empty container.
Is this a good strategy or there are better ways?
Seems you`re looking for something like this:
{% set toolbar_tools = {
'font':'<select class="ql-font"></select>',
'size':'<select class="ql-size"></select>'
}
%}
<div id="button-container">
<span class="ql-formats">
{% if toolbar|length > 0 %}
{% for t in toolbar %}
{% if t == 'all' %}
{# show all options #}
{% for tool in toolbar_tools %}
{{ tool|raw }}
{% endfor %}
{% else %}
{# show defined options #}
{{ attribute(toolbar_tools, t)|raw }}
{% endif %}
<br />
{% endfor %}
{% endif %}
</span>
</div>
Hope you will be fine with that.

SonataUserBundle override user profile

i want to override my user dashboard [profile show.html.twig], but i don't know how to do it. Someone can explain me or show me his code, so i can have an idea how he did it.
this is my show.html.twig:
{% extends "SonataUserBundle:Profile:action.html.twig" %}
{% block sonata_profile_content %}
{% sonata_template_box 'This is the user profile template. Feel free to override it.' %}
<div class="row row-fluid">
{% set has_center = false %}
{% for block in blocks %}
{% if block.position == 'center' %}
{% set has_center = true %}
{% endif %}
{% endfor %}
<div class="{% if has_center %}span4 col-lg-4{% else %}span6 col-lg-6{% endif %}">
{% for block in blocks %}
{% if block.position == 'left' %}
{{ sonata_block_render({ 'type': block.type, 'settings': block.settings}) }}
{% endif %}
{% endfor %}
</div>
{% if has_center %}
<div class="span4 col-lg-4">
{% for block in blocks %}
{% if block.position == 'center' %}
{{ sonata_block_render({ 'type': block.type, 'settings': block.settings}) }}
{% endif %}
{% endfor %}
</div>
{% endif %}
<div class="{% if has_center %}span4 col-lg-4{% else %}span6 col-lg-6{% endif %}">
{% for block in blocks %}
{% if block.position == 'right' %}
{{ sonata_block_render({ 'type': block.type, 'settings': block.settings}) }}
{% endif %}
{% endfor %}
</div>
</div>
{% endblock %}
Thank you.
You can override the show template in app/Resources/SonataUserBundle/views/Profile/show.html.twig.
You must clear caché at the end

flashMessage on redirect on Symfony2 not working

I have the following code in my controller:
$this->get('session')->getFlashBag()->add(
'storeinfo',
'hayooo'
);
return $this->redirect($this->generateUrl('AppMainBundle_item_detailed_view', array('id' => $picture->getId(), 'caption' => $picture->getURLCaption())), 301);
and my twig looks like this:
{% if app.session.flashbag.get('storeinfo') %}
<div class="comment-confirmation">
{% for flashMessage in app.session.flashbag.get('storeinfo') %}
<p> <b> anjing banget </b></p>
{% endfor %}
</div>
</div>
{% else %}
<p> Oopsie </p>
{% endif %}
so it goes to the first if block however inside the forloop, there is no flashMessage. Why is this?
The FOSUserBundle does something like this, you could also have a look at this great imlementation of a FlashListener
{% for type, messages in app.session.flashbag.all() %}
{% for message in messages %}
{# Will print all your messages #}
{{ message }}
{# Will do something in particular for storeinfo if you want to #}
{% if type is sameas('storeinfo') %}
<p> <b> anjing banget </b></p>
{% endif %}
{% endfor %}
{% endfor %}

How to get form variables in custom form field in symfony2

I have this custom form field
{# src/Acme/DemoBundle/Resources/views/Form/fields.html.twig #}
{% block gender_widget %}
{% spaceless %}
{% if expanded %}
<ul {{ block('widget_container_attributes') }}>
{% for child in form %}
<li>
{{ form_widget(child) }}
{{ form_label(child) }}
</li>
{% endfor %}
</ul>
{% else %}
{# just let the choice widget render the select tag #}
{{ block('choice_widget') }}
{% endif %}
{% endspaceless %}
{% endblock %}
This renders the checkboxes. But i am not able to find how can i get the value of checkbox
i.e child.form.value is not working.
Suppose i have entities which is named as tasks in the form.
how can i get the value of the taskid.
something like
child.form.vars.task.id
It seems to be in {{ choice.value }}
Have a look at this to see how the inheritance works.
<option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice.label|trans({}, translation_domain) }}</option>

Categories