Symfony - 重定向到填写了表单值的操作

发布于 2024-11-03 16:21:53 字数 570 浏览 3 评论 0原文

如何重定向到另一个操作,并在新操作上填写表单字段?

我有一个操作,我想调用redirect(),例如:

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

哪个应该重定向到我的搜索操作,并且该操作中的表单应该填写last_name参数。该操作的代码如下:

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

SearchForm具有名称格式“搜索[%s]”。

我对重定向()的参数所做的任何尝试都不起作用。 Search[] 参数总是会以某种方式搞乱并且不会填充表单。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蒲公英的约定 2024-11-10 16:21:53

我会尝试让 Symfony 根据路由和参数为您生成 URL:

@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);

参数数组应采用以下格式...

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

...因此 URL 将它们视为:

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

然后您应该能够使用.. ...

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

甚至可能...

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

希望对您有帮助或至少给您一些想法。

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.

花间憩 2024-11-10 16:21:53

尝试此解决方案

另一种方法是将提交数据存储在用户会话中。

Try this solution.

Another method is to store submission data in user session.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文