Symfony 2, form and jQuery on change submit - php

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.

Related

DELETE request on a link in laravel 5.4

I am trying to hit a route I made to make a delete HTTP request in laravel view when user clicks on 'Delete' button, but it won't work. I've read it should be done with forms in laravel.
Here is my code:
<form action="/admin/pages/delete/{{ $section->id }}" method="post">
{{ method_field('delete') }}
<button class="btn btn-sm" type="submit">Delete</button>
</form>
What is a proper way to handle this?
It shows me an error in the console, Bootbox: 'please specify a message' whenever I click on the button.
Route definition inside admin group:
Route::delete('/pages/delete/{id}', 'PagesController#delete')->name('pages.delete');
I believe you are missing the csrf token in the form.
You can add
{{ csrf_field() }}
just after your form starts.
Visit this link for knowing more about csrf
You must add the the CSRF Field because all form submission must past through the VerifyCsrfToken middleware before the request be procede by the controller
{{ csrf_field() }} // add this before or after the {{ method_field() }}

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.

Delete handled by resource controller doesn't work - Laravel 5.3

I'm using resource controller in Laravel 5.3 and I'm having problem with deleting a record. I would like to use simple HTML code and I know that I have to add a hidden method input to make it work.
My code is very simple:
<form action="{{ url('/task', $task->id) }}">
{{ method_field('DELETE') }}
<input type="submit" value="Delete" />
</form>
After clicking submit app redirects to blank page - it doesn't go to destroy function in controller. I don't have any idea, why it's not working. I'm not using facades, is it necessary in operation like this? I'll be very glad for every tip, thank you.
You're most likely running into a TokenMismatchException. Laravel considers the DELETE method a "writable" method, so it expects a CSRF token.
You can either add a CSRF token to your form, or, if appropriate, you can add your URI to the except array in your app/Http/Middleware/VerifyCsrfToken.php file.
To add the token to your form:
<form action="{{ url('/task', $task->id) }}">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<input type="submit" value="Delete" />
</form>

How can I get TokenMismatchException when trying to submit the form?

How can I place a token in laravel, I am getting a token error? Thank you for your collaboration.
I'm guessing the reason way Jeffery's code works is because the version of Laravel doesn't have default csrf protection, so if u want to make your form works, you have to manually add :
<input type="hidden" name="_token" value="{{ csrf_token() }}">
or create the form with the form helper:
{{ Form::open(array('url' => 'foo/bar')) }}
//
{{ Form::close() }}
which will automatically generate the hidden input tag of csrf token

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