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>
Related
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.
I'm building a web page in Symfony that is built with multiple instances of the same form, but with different POST urls. I want to intercept the submit with javascript, and do a simple AJAX POST.
The structure of this page is this:
<h1>Title</h1>
{% for document in documents %}
<div id="form_{{ document.id }}">
{{ form(document.form) }}
<input type="submit">Submit button</input>
</div>
{% endfor %}
<hr />
If I have 4 documents in the documents array, 4 forms will be rendered. The problem is that all the documents fields will have the same ID: there will be 4 document_csrf, 4 document_title title, and so, because all documents are views of the DocumentType form.
What I want is to add something to the generation of the form, so each form field is rendered with unique fieldnames: document_csrf1, document_title_1, document_csrf2, document_title_2, etc.
I've been looking around StackOverflow and the Symfony Documentation, but I can't find anything. I can pass an ['attr'] => ['id' => $id]] then calling the createForm function, but this ID is only used for the <form> tag, not the other fields...
Is there any way to automatically add an ID to the form, or should I render the form adding the id to each element ({{ form_widget(form.name, {'attr': {'id': 'document_name' ~ document.id}}) }})?
Thank you for your help!
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.
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 ?
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