Boubouh Karim Boubouh Karim - 2 months ago 14
PHP Question

Symfony, Keep Get Argumments in Search form after submit

Hi,

i have created a search form using GET method, to my indexAction, the form contains virtual fields.

i created the logic for searching inside the indexAction.

My problem is when i submit the form, all inputs values become empty.

What i want is to keep the values passed as arguments in form inputs

here is my FormType :

public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('title', TextType::class, array(
'required' => false,
'attr' => array(
'placeholder' => 'Chercher',
)
))
->add('client', EntityType::class, array(
'required' => false,
'class' => 'AppBundle:Person',
'placeholder' => 'Choisir client',
'attr' => array(
'class' => 'select2',
)
))
->add('date_start', DateTimeType::class, array(
'required' => false,
'input' => 'datetime',
'widget' => 'single_text',
'format' => 'dd-MM-yyyy',
'inherit_data' => true,
'attr' => array(
'name' => 'date_start',
'class' => 'datepicker',
'placeholder' => 'Date d├ębut',
'value' => ''
)
))
->add('date_end', DateTimeType::class, array(
'required' => false,
'input' => 'datetime',
'widget' => 'single_text',
'format' => 'dd-MM-yyyy',
'inherit_data' => true,
'attr' => array(
'name' => 'date_end',
'class' => 'datepicker',
'placeholder' => 'Date fin',
'value' => ''
)
))
;
}


The indexAction :

public function indexAction(Request $request)
{
$getArgs = $request->query->get('order');
dump($getArgs);
$form = $this->createForm('AppBundle\Form\Search\OrderType', new Order(), array('get' => $getArgs));
$form->handleRequest($request);

$repo = $this->getDoctrine()->getRepository('AppBundle:Order');
$query = $getArgs ? $repo->findAllMatched($q) : $repo->createQueryBuilder('o');

$paginator = $this->get('knp_paginator');
$orders = $paginator->paginate($query);

return $this->render('AppBundle:Order:index.html.twig', array(
'orders' => $orders,
'form' => $form->createView(),
));
}


Any suggestions are wellcome.

Answer

For now, i resolved the problem in this way :

            <div class="nav box-search">
            <form method="GET" class="form-inline">
                <div class="pull-right">
                     <button type="submit" class="btn btn-default">{{ 'actions.search' | trans }}</button>
                </div>
                <div class="pull-left">
                    {% set params = app.request.query.all %}
                    {{- form_widget(form.title, { 'attr': {'value': params.order.title| default('')} }) -}}
                    {{ form_widget(form.client,  {value: params.order.client| default('') }) }}
                    {{- form_widget(form.date_start, { 'attr': {'value': params.order.date_start| default('')} }) -}}
                        <i class="fa fa-exchange"></i>
                    {{- form_row(form.date_end, { 'attr': {'value': params.order.date_end| default('')} }) -}}
                </div>
            </form>
        </div>

It's not that bad solution, but what i would is to have this parameters configuration in the FormType.