Setting selected attribute for loop with key and value - php

I made a select box which accepts an associative array with key and value pairs. It only shows the value, but it passes the key to a javascript variable, which in turn is needed for an api call.
When the user has made the api call, the id is stored in an url parameter. I want to use this, to set the selected option in the dropdown when the page reloads. This is the code:
<select ng-model="ctrl.filterSettings.selectedCompany" ng-change="ctrl.init({{ dashboard.year }}, {{ dashboard.month }}, ctrl.filterSettings.selectedCompany)">
{% for id, name in companies %}
{% if id == selectedCompanyId %}
<option value="{{ id }}" selected="selected">{{ name }}</option>
{% else %}
<option value="{{ id }}">{{ name }}</option>
{% endif %}
{% endfor %}
</select>
This works as intended. If in the url parameter I have an id of let's say 250, and that id is also part of the companies array, it sets the selected to that option. However, it also sets selected to the first option which seems to be generated by the for loop:
<option value="? string: ?"></option>
This is the first empty option, where nothing is selected. But I want this removed when I have a companyId in the parameters.
I have tried to remove the value from the options using javascript, but if I try that, the first option of my array is removed.
Any help is greatly appreciated.

Related

Is this possible to validate a form created without formBuilder?

As I say in the title. I have create a form in a twig file
<form action="{{ path('domain_confirm') }}" method="post" id="choose-domain-form">
{% for domain, price in data %}
<tr>
<td>
{{ domain }}
</td>
<td></p>{{ price }} €</td>
<td><input class="domain-purchase-checkbox" type="checkbox" name="domaintest" value="{{ domain }}" data-price="{{ price }}"></td>
</tr>
{% endfor %}
</form>
How can I validate this form and get datas from this form inside my Controller ?
When I try to get these datas with
$request->request->get('form');
I get a null response
Do you have some ideas ?
As for the validation, you will have to create a form with the formBuilder and validate it that way. You can however, validate your input using the Symfony Validators, but this is not advised.
I strongly suggest using the provided form builder as it has alot of features you want such as CRSF and well... validation, aswel as auto mapping onto your entities.
As for getting the data in this specific case you will need to call :
$request->request->all();
However, i see all your checkboxes will have the same "name" attribute, you will need to change it from name="domaintest" to name="domaintest[]".
When you call :
$request->request->get('domaintest');
You will receive an array with values.

outputting old form data array with Twig

I am programming a form. I am using a Select2 to allow the user choose multiple options in the select tag. If there is another error, I redirect the user back to the form and I keep the values the user had already either typed or chosen, so that he doesn't have to fill the whole form again.
Everything is fine in the rest on the fields, since I can retrieve the posted info by just using the request.post('input_name') function.
When it comes to these multiple choices selects, I know I get an array.
Somehow I know there are indeed values posted in array if I just make the following test:
{% if request.post('select2inputMultiple') %}
<p>Data have been posted from select2 multiple</p>
{% endif %}
However, If I try to display (output) the data like this:
{{request.post('select2inputMultiple')}}
It throws the following error:
An exception has been thrown during the rendering of a template ("Array to string conversion")
How can I access the items of that array?
Well, It looks that it is working, I am trying with a foreach function like this:
{% if request.post('select2inputMultiple') %}
<p>Data have been posted from select2 multiple</p>
{% for single in request.post('select2inputMultiple') %}
value: {{single}}
{% endfor %}
{% endif %}
And it is outputting the data as desired!
Assuming your input is named select2inputMultiple[], request.post('select2inputMultiple') is an array (as the error indicates). You can't display an array on your page without an intermediary to convert it to a string. Easiest way to view the value from Twig is to use the dump method, which maps to var_dump. So you would do
{{ dump(request.post('select2inputMultiple')) }}
Assuming you have a select structure like this:
<select name="select2inputMultiple[]">
{% for option in options %}
<option value="{{ option.id }}">{{ option.name }}</option>
{% endfor %}
</select>
The easiest way to select those options from that array would be something like:
<select name="select2inputMultiple[]">
{% for option in options %}
<option value="{{ option.id }}"
{% if option.id in request.post('select2inputMultiple') %}
selected
{% endif %}
>{{ option.name }}</option>
{% endfor %}
</select>

multiple form using twig

I was wondering, is there a way to display several forms using a single controller ?
I got a list of items. I get them all by using Doctrine findAll, and return them using the controller.
I display them by using twig :
{% for item in items %}
<form method="post" {{ form_enctype(dislike) }}>
{{ form_widget(dislike) }}
</form>
{% endfor %}
For every item displayed, I got a div with a simple form button.
Unfortunately this doesn't work at all ! it only displays a button in the very first div, even though I got several items to display.
Any idea why ?

Save multiple instances of one entity with form submit in symfony2

Right now I'm rendering two forms ( one for company and one for it's tags ) and it looks like this:
<h3>Company</h3>
{{ form_start(form) }}
{{ form_row(form.name) }}
{{ form_row(form.city) }}
{{ form_row(form.street) }}
{{ form_row(form.postalcode) }}
{{ form_row(form.buildingnumber) }}
{{ form_row(form.vatid) }}
{{ form_row(form.tags) }}
<button id="test">Test</button>
{{ form_row(tags_form.title) }}
{{ form_row(form.save) }}
{{ form_end(form) }}
I want users to be able to add another input ( after clicking test button ) {{ form_row(form.tags) }} so they can add multiple tags with one form, but right now my tags form looks like this:
$builder
->add('title',null,array(
'label' => 'tag.title',
'required' => false
));
and I don't really know how to set this up. I tried with the simplest solution:
$('#test').on('click',function(e) {
e.preventDefault();
$('#fourcreate_portalbundle_companytags_title').clone().appendTo('form');
});
but that way submitting form creates entity only from the second input.
EDIT: I forgot to add - it has to be done with two forms, because first form contains list of currently available tags and the second form is to let users add their own.
You should not use two forms, but have a collection of tags_form in form.tags. The sample in the Cookbook is about adding tags.
The browser sends a "clobbered" form where two or more name attribute of two or more input elements have the same value. Hence, the back-end gets only the last value. I can not be more specific because I am not familiar with that part of Symfony.
If you wish to clone input elements, and have their values submitted correctly, you must at least modify the name attributes before submitting the form. Also, watch out for non-unique id attributes, as that violates the HTML (DOM?) standard.
e.g
var clone = $('input[id="original"]').clone();
clone.attr('name', clone.attr('name') + '1');

How to customize collection element in symfony2 forms

I have a simple form related to a user in which i'm adding a collection of phone numbers (another form).
I can get it working with javascript but i want to customize the rendering of each phone input.
The same thing is working with a simple field by doing this :
{% block _elementId_widget %}
{{ block('form_widget_simple') }}
<input type="button" class="btn" id="btnInvertNames" value="{{ "form.invertNames"|trans }}" />
{% endblock %}
To add a button after the widget, but not working with collections..
A solution i found is to write a new "Form Type" and to customize its template :
Look here for documentation

Categories