Lukas Hirt
Lukas Hirt

Reputation: 48

Symfony4 - change single parameter in url after submitting form

I have a route that looks like this - route/{parameter} and I need to change the parameter after submitting a form.

I tried to use redirectToRoute but it created new URL together with some other parameters that the form passed which I don't want.

So I would love to ask you if there is some way to redirect to a new URL with the only parameter that I choose through select in the form.

Thank you very much for your responses.

EDIT:

I am going to share more actual information. This is how my controller for the form looks like:

$form = $this->createFormBuilder()
    ->setMethod("get")
    ->add('category', ChoiceType::class, [
        'choices' => [
            'Všechny kategorie' => 'vsechny-kategorie',
            'Automobilový průmysl' => 'automobilovy-prumysl',
            'Stavebnictví' => 'stavebnictvi',
            'Elektronika a elektrotechnika' => 'elektronika-a-elektrotechnika',
            'Gastronomie' => 'gastronomie',
            'Lesnictví' => 'lesnictvi',
            'Potravinářský průmysl' => 'potravinarsky-prumysl',
            'IT technologie' => 'it-technologie',
            'Logistika' => 'logistika',
            'Strojírenství' => 'strojirenstvi',
            'Zdravotnictví' => 'zdravotnictvi'
        ],
        'label' => 'Kategorie:'
    ])
    ->add('send', SubmitType::class, ['label' => 'Test'])
    ->getForm();

$form->handleRequest($request);

if($form->isSubmitted() && $form->isValid()) {
    $data = $form->getData();
    $category = $data['category'];

    return $this->redirectToRoute('jobs', [
        'jobs' => $pagination,
        'categoryForm' => $form->createView(),
        'category' => $category,
    ]);
}

Upvotes: 0

Views: 174

Answers (2)

Dylan Pierce
Dylan Pierce

Reputation: 4668

You should be able to use the redirectToRoute, but be sure to pass the parameter you're trying to dynamically set as an array:

// in your controller action:
return $this->redirectToRoute('post_form_route', ['parameter' => $parameter]);

If that's not working for you, I would double check your route definitions and make sure your route's name & expected URL parameters are passed correctly.

Documentation on redirecting in the controller

Upvotes: 2

Themer
Themer

Reputation: 659

you can try that :

if($form->isSubmitted() && $form->isValid()) {
    $data = $form->getData();
    $category = $data['category'];

    return $this->redirectToRoute('route', [
         'parameter' => $form->getData()->getCategory()
     ]);

}

return $this->redirectToRoute('jobs', [
    'jobs' => $pagination,
    'categoryForm' => $form->createView(),
  ]);

Upvotes: 0

Related Questions