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!
Related
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 ?
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');
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
In Symfony docs they say to use this
<div>
{{ form_label(form.task) }}
{{ form_errors(form.task) }}
{{ form_widget(form.task) }}
</div>
But this generates the label element.
But I want to have table <td> instead of <label>
And also for the input textbox, I want to mention the size of text box. Where can I do that?
You have to define your form theme.
Probably, this tutorial is what you are looking for
I have this in template
<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register">
{{ form_widget(form) }}
The form is appearing as
fos_user_registration_form_username --input box
fos_user_registration_form_email
fos_user_registration_form_plainPassword_first
fos_user_registration_form_plainPassword_second
But it want to have simple labels like Username , Email etc. How to do that
You can parse the individual form fields instead of the form as a whole.
{{ form_widget(form.fos_user_registration_form_username) }}
In this way, you can parse a single form element. Make sure to end with
{{ form_rest(form) }}
to output any not yet parsed fields (such as the csrf protection token).
using the above approach you can add your own labels to the fields.
FOSUserBundle uses the translator system. You need to as seen in the docs here:
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/1.2.0/Resources/doc/index.md
Add this:
# app/config/config.yml
framework:
translator: ~
To your config file (app/config/config.yml).
This will tell symfony to replace all the label values with the values found in the translator file (FOSUserBundle.en.yml). Then the form will always print "Username" instead of "fos_user_registration_form_username".