matt
matt

Reputation: 1983

Symfony - Redirect to action with form values filled in

How can I do a redirect to another action, and get the form fields to be filled in on the new action?

I have an action and I want to call redirect() something like:

$this->redirect('foo/search?Search[last_name]='.$this->form->getValue('last_name'));

Which should redirect to my Search action, and the form in that action should get the last_name parameter filled in. That action has code like so:

public function executeSearch(sfWebRequest $request)
{
   $this->form = new SearchForm();
   $submission = $request->getParameter($this->form->getName());
   ...do stuff with $submission...

The SearchForm has the name format 'Search[%s]'.

Nothing I've tried for the parameter to redirect() has worked. The Search[] parameters always get messed up in some way and will not populate the form.

Upvotes: 1

Views: 3571

Answers (2)

Tom
Tom

Reputation: 30698

I would try to get Symfony to generate the URL for you based on the route and parameters:

@search = a route for your search action in your routing.yml

$parameters = array of the parameters you want to pass

$url = $this->generateUrl('search', $parameters);
$this->redirect($url);

The array of parameters should take the following format...

'Search[something1]' => 'value1'
'Search[something2]' => 'value2'

... so the URL takes them as:

?Search[something1]=value1&Search[something2]=value2

You should then be able to use...

$this->form->bind($request->getParameter('Search'));
(for binding)

... and probably even...

$this->form->setDefaults($request->getParameter('Search'));
(for populating for a template)

Hope that helps or at least gives you some ideas.

Upvotes: 2

Darmen
Darmen

Reputation: 4881

Try this solution.

Another method is to store submission data in user session.

Upvotes: 0

Related Questions