I would like know how can I change this:
<select id="mifid-proprio" name="mifid-proprio">
<option value="{{ personne.id }}">{{ personne.prenom }} {{ personne.nom }}</option>
{% for key, proprietaire in proprietaires %}
<option value="{{ proprietaire.destination.id }}">{{ proprietaire.destination.prenom }} {{ proprietaire.destination.nom }}</option>
{% endfor %}
</select>
into Twig + Formbuilder (Symfony 2).
I think you want to build that widget in the FormBuilder. If I am right you should take a look at the documentation of the choice Field Type. When the data you want to display in that select box comes from a database you should take a look at the entity Field Type. If you have never worked with the FormBuilder before, you should also read how to create form classes.
If you build that widget in the FormBuilder you can output it in Twig with:
{{ form_widget(form.field_name) }}
When you are not satisfied with the generated HTML code you can overwrite it, either on a per-template or global basis. You can read more about form theming.
Related
I got a custom taxonomy of events called "typology".
Taxonomy :
slug : typology
name : Typologies
- type 1
- type 2
- type 3
I created a dynamic selector that displays each type in order to filter a list of post based on that specific taxonomy.
This part is OK with :
$context['terms-typology'] = Timber::get_terms('typology');
and in twig
<select>
{% for term in typology %}
<option value="{{ term }}">{{ term }}</option>
{% endfor %}
</select>
But i would like to add a first option (selected by default) which would be an unfiltered option and where the text should be : "All Typologies".
I tried to add this option and get back the parent {{ term.name }}
I tried to pass it in the context with another line :
$context['term'] = new Timber\Term('typology');
And use
<option selected value="{{ term.name }}">All {{ term.name }}</option>
I've tried many other things. But I just don't manage to do it and I don't get it.
I am building a form which contains a series of categories used to search some content, defined as a ChoiceType within my Symfony FormType.
I pass the category list along with some other data (per-category count) (defined as the variable "aggs" in my controller) into a Twig template and create a form theme, overriding the choice_widget_options block for my categories drop-down so that I can display the extra data at render time, thus:
{% form_theme form.categories _self %}
{% block choice_widget_options %}
{% if choices is defined %}
{% for choice in choices %}
<option value="{{ choice.value }}">{{ choice.label }} {{ aggs }}</option>
{% endfor %}
{% endif %}
{% endblock choice_widget_options %}
Why is it that my block here cannot access the top-level variables defined in my controller? It can see the "app" global variable, but not the controller-defined ones.
Thanks!
More elegant solution is to override the buildView method of AbstractType class.
Just add:
$view->vars[‘aggs’] = YOUR AGGS VAR;
And the use it in your form as:
<option value="{{ choice.value }}">{{ choice.label }} {{ form.vars.aggs }}</option>
Basically you pass the variable to your form and then you use it in twig. The way you pass it to the form type is up to you. It can be a dependency injection or via the form options from the controller.
As i said in title i have this
<select id="form_test" oninput="loadTemplate()">
<option>Template list</option>
{% for template in templates %}
<option id="test" value="{{ template.getReport }}">
{{ template.getTemplateName }}
</option>
{% endfor %}
</select>
It shows me all template names from {{ template.getTemplateName }} from database. However value="{{ template.getReport }}" this value always returns first report from database, as If for loop won't work inside tag.
I am using doctrine to fetch this entities from database, like this:
$templates = $this->getDoctrine()->getRepository(ReportTemplate::class)->findAll();
return $this->render('report_form/reportForm.html.twig', [
'templates' => $templates,
]);`
After I deployed site on live server it started working. It's strage I still don't know what was wrong.
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 %}
Code of rendered Twigs radio button set form usually looks like...
This:
{{ form_widget(formTitle.radioSet) }}
To this...
<input type="radio" id="formTitle_radioSet_0" name="formTitle[radioSet]" value="fooValue">
<input type="radio" id="formTitle_radioSet_1" name="formTitle[radioSet]" value="booValue">
... and a few more rows like this
I want to make it work with JS easier. Is it possible to render customized radio buttons with IDs reffered to the value of radio and my own preferences like this??
<input type="radio" id="radioSet_fooValue" name="formTitle[radioSet]" value="fooValue">
<input type="radio" id="radioSet_booValue" name="formTitle[radioSet]" value="fooValue">
After about half an hour of exploring article recommended by Carlos Granados got a solution. The target is to override twigs template of renderind radio inputs.
In a few words, code below, pasted at the beginning of your web page template will override ALL radio's render the way it described in question.
{% form_theme form _self %}
{%- block radio_widget -%}
<input type="radio" name="{{ full_name }}" id="radio_{{ value }}" value="{{ value }}"{% if checked %} checked="checked"{% endif %} />
{%- endblock radio_widget -%}
Use custom form rendering
You will just need to customize the widget_radio block using one of the techniques described in that cookbook entry