I use a search form in Symfony 4.2.5, with GET method.
But the URL is not very... sexy.
I want to get a clean URL.
I have already disabled CSRF protection, and removed the submit from the FormBuilder ( otherwise, the submit button was ALSO on the URL).
The form :
public function searchForm()
{
//Form search creation
$form = $this->createFormBuilder(null, array('csrf_protection' => false))
->setAction($this->generateUrl('page'))
->setMethod('GET')
->add('object', TextType::class)
->getForm();
return $this->render('page.html.twig', ['searchForm' => $searchForm->createView()]);
}
The view :
<form class="search">
{{ form_start(searchForm) }}
{{ form_row(searchForm.object, {'attr' : {'placeholder': "Search..."}}) }}
<button id="searchSubmit" class="btn btn-success">Search </button>
{{ form_end(searchForm) }}
</form>
With this code, I get localhost/page?form[object]=SearchTerm
I know, it is a detail, but I want to get an URL like localhost/page?object=SearchTerm.
Related
I'm trying to make a form, which I self-defined the action like this in my controller:
$form = $this->createForm(ProgrammeSearchType::class, $search, [
'action' => $this->generateUrl('recherche_programme'),
'method' => 'GET',
]);
But, the form rendered in the view look like this:
<form id="myForm">
{{fields.....}}
</form>
So.. there is a problem. Why "action" is not specified in the HTML while I defined it into the controller.
Regards
Symfony doc: https://symfony.com/doc/current/forms.html#changing-the-action-and-http-method
Use {{ form_start(form) }} and {{ form_end(form) }} instead of <form> ... </form> tags in your view template.
I want to implement a form in Symfony that filters a data set for me.
The extract of my twig file as well as the the FilterType that is used for the form are shown below.
list.html.twig
{% block filterContent %}
{{ form_start(form) }}
<div class="row">
{#This renders a red banner if the form contains errors.#}
{#If the form variable is not called "form", pass it explicitly.#}
{% include 'Form/form_errors_banner.html.twig' with {'form': form} %}
{{ form_row(form.component) }}
{{ form_row(form._token) }}
{{ form_errors(form) }}
</div>
<input type="reset" id="resetter" class="btn-primary btn btn-xs" value="{{ 'label.resetAll'|trans }}"/>
<input type="submit" class="btn-primary btn btn-xs" value="{{ 'label.applyFilter'|trans }}"/>
{{ form_end(form) }}
<!-- <br clear="all" /> -->
{% endblock filterContent %}
FilterType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->setAction($options['data']['url'])
->setMethod('GET')
->add('component', ChoiceType::class, array(
'label' => 'Component',
'placeholder' => 'Select Component',
'choices' => array(
'All' => 'All',
'Document' => 'Document',
'User' => 'User',
'Waiver' => 'Waiver'
),
'required' => false,
));
}
For some reason I get the following error message indicating that my CSRF token is invalid.
CSRF invalid error message
After having a closer look into this, I suspect that the error is caused, because the CSRF token in the transmitted URL differs from the one that is sent with the form:
CSRF token mismatch
I already tried to remove {{ form_row(form._token) }} line in my twig file and cleared my browser and server cache, but still the error message pops up.
Could anyone help me and tell me what I did wrong?
In FilterType.php, ensure that you are using the same ID for your token as in your twig form. You can customize it via configureOptions:
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'csrf_token_id' => FilterType::class
]);
}
I know that this question may have been made but I just can't get it to work. if someone could help me I would be very grateful. I have colletive/form installed but the answer can be an html form tag too.
Now listing my form, my route and my exception.
{{ Form::model( array('route' => array('casas.update', 238), 'method' => 'PUT')) }}
<input type="hidden" name="_method" value="PUT">
-
Route::resource('casas', 'CasasController');
exception:
MethodNotAllowedHttpException in RouteCollection.php line 218:
With plain html / blade
<form action="{{ route('casas.update', $casa->id) }}" method="post">
{{ csrf_field() }}
{{ method_field('put') }}
{{-- Your form fields go here --}}
<input type="submit" value="Update">
</form>
Wirth Laravel Collective it may look like
{{ Form::model($casa, ['route' => ['casas.update', $casa->id], 'method' => 'put']) }}
{{-- Your form fields go here --}}
{{ Form::submit('Update') }}
{{ Form::close() }}
In both cases it's assumed that you pass a model instance $casa into your blade template
In your controller
class CasasController extends Controller
{
public function edit(Casa $casa) // type hint your Model
{
return view('casas.edit')
->with('casa', $casa);
}
public function update(Request $request, Casa $casa) // type hint your Model
{
dd($casa, $request->all());
}
}
I've been learning laravel 5.2 recently, and i've made a delete function which should delete records from my database but instead of deleteing the records it's adding a blank row into my database
This is the Route im using:
Route::resource('producten', 'ProductenController', ['only' => ['index', 'store', 'destroy', 'edit', 'update', 'create']]);
This is the controller function i use for it
public function destroy(request $request , product $product)
{
$product->delete();
return redirect(Route('producten.index'));
}
This is the form i've made for it.
{{ Form::Open(['Route' => 'producten.destroy', $product], ['method' => 'delete']) }}
{{ Form::Submit('delete')}}
{{ Form::close() }}
when i viewed the source-code it said it was using a POST method instead of a delete method, and also when i add($product) i got a blank page, also i found out that when i hit the submit button it goes to the store method i've made and i dont know why,
if u need more information just let me know and i'll add it in the question
route and method should be in the same array, not in two differents arrays.
{{ Form::Open(['method' => 'DELETE', 'route' => ['producten.destroy', $product]]) }}
{{ method_field('DELETE') }}
{{ Form::Submit('delete')}}
{{ Form::close() }}
I think you have something wrong with form. Can you try with this:
<form action="{{ route('producten.destroy', ['product' => $product->id]) }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit">Remove</button>
</form>
I am trying to make a search by date form to work on Symfony 2.3. I have an entity with a few fields (5) the name of the entity is Schedule and two of this fields are datetime, for start date time and end Date time. I want to search by dates, but it is giving me headaches.
I have this action:
public function indexAction(Request $request)
{
//time form creation
$aSchedule = new Schedule();
$dateTimeForm = $this->createFormBuilder($aSchedule)
->add('startDateTime', 'datetime')
->add('endDateTime', 'datetime')
->add('search', 'submit')
->getForm();
//getting the formr using post
$dateTimeForm->handleRequest($request);
if ($dateTimeForm->isSubmitted()){
echo 'Submited';
}
if ($dateTimeForm->isValid()){
echo 'Is Valid';
}
}
I have show the form in template like this:
<form action="{{ path('osd_sch_homepage') }}" method="post"
{{ form_enctype(dateTimeForm) }} >
<div id="start-date-time">
{{ form_label(dateTimeForm.startDateTime) }}
{{ form_errors(dateTimeForm.startDateTime) }}
{{ form_widget(dateTimeForm.startDateTime) }}
</div>
<div id="end-date-time">
{{ form_label(dateTimeForm.endDateTime) }}
{{ form_errors(dateTimeForm.endDateTime) }}
{{ form_widget(dateTimeForm.endDateTime) }}
</div>
<div>
{{ form_widget(dateTimeForm.search) }}
</div>
</form>
Now in the action every time in send the form the "$dateTimeForm->isSubmitted()" works fine, but the "$dateTimeForm->isValid()" is not getting true, I mean is never going the "echo 'Is Valid';". what am I doing wrong?
Thank you in advanced.
Abel Guzman
It's probably not putting the auto generated CSRF token. Try putting {{ form_rest }} at the end.
Have you tried to debug your form errors?
foreach($form->getErrors() as $err){
echo $err->getMessage();
}