0

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,
    ]);
}
4
  • Are you trying to dynamically change the form's endpoint before the POST action or are you trying to change the redirection of the user after the endpoint has processed the form? Commented Oct 11, 2018 at 20:00
  • @DylanPierce I'm making a filtration for a list. The filtration works fine but I need to write the name of the category that is used as a filter in the URL so it looks for example like this domain.com/list/category Commented Oct 11, 2018 at 20:58
  • is $category a string or is it an object? Commented Oct 15, 2018 at 14:50
  • It is a string. Commented Oct 15, 2018 at 21:21

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

The problem is that it returns URL that is longer than even all the characters available for this comment. This is how the code I have: return $this->redirectToRoute('jobs', [ 'jobs' => $pagination, 'categoryForm' => $form->createView(), 'category' => $category ]);
Hard to tell, but I would guess that one of the URL params you're passing is a very large object, but instead is supposed to be a single attribute or a small array.
I pass the parameter category and it's in the URL as it should be but then it also passes all the info about the form as a parameter, name of the form, its children, selected value ... And that's what I'm trying to prevent (unsuccessfully yet).
0

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(),
  ]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.