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.
Related
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!
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
I want to create form with auto submit, so I used jquery. I think that i have problem with translate url.
My form :
<form action="edit" method="get" >
<select name="id" onchange="this.form.submit();">
{% for panel in panele %}
<option value="{{ panel.setting.id }}">{{ panel.nazwa }}</option>
{% endfor %}
</select>
</form>
And when I want to change option, I get this in url "edit?id={id}", and when i try use it in controller (route).
#Route("/edit?id={id}")
I got error:
No route found for "GET /settings/edit"
(/settings is global route for controller)
The action attribute of your form has no URL. Your should generate the url using twig path expression.
<form action="{{ path('edit_route') }}" method="get" >
Replace the route identifier edit_route with the name of your route. Also, clear browser and symfony caches.
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