multiple form using twig - php

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 ?

Related

How can I customize form fields IDs in Symfony 4?

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!

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.

Symfony 3 prevent full form rendering twig

I'm currently trying to only render specific parts of a form but for some reason, the whole form keeps getting rendered as you can see below I've used the form start and end and the whole form is still being rendered even though it should only render the start and end of the form tags.
{{ form_start(searchForm) }}
{{ form_end(searchForm) }}
Also whenever I use the code below all that happens is that the elements are being moved to the top of the form and the elements which shouldn't be rendered are rendered below.
{{ form_start(searchForm) }}
{{ form_widget(searchForm.title) }}
{{ form_widget(searchForm.title) }}
{{ form_end(searchForm) }}
As described in the doc, for don't render unrendered fields:
{{ form_end(form, {'render_rest': false}) }}
Hope this help

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