在表单验证后,将一长串元素作为参数传递给Symfony重定向

时间:2022-10-24 14:53:42

I have a form in which a user can upload a CSV file containing emails. Those emails get processed and sent, and for the ones that passes the validation I create an array of "$successfulEmails".

我有一个表单,用户可以上传包含电子邮件的CSV文件。这些电子邮件得到处理和发送,对于通过验证的电子邮件,我创建了一个“$ successfulEmails”数组。

In my first implementation I passed that to a twig rendering it, and it was ok (I could display in my twig "These are the emails that were sent" with the list following). The only issue is that in the landing page I could hit the refresh button, resubmitting the form. So, as suggested in Symfony documentation, it's preferrable to have a redirect at the end of the form->isValid(). The problem with redirect is that it goes through Symfony's routing system: I can pass a few variables doing something like

在我的第一个实现中,我将它传递给了一个渲染它的树枝,它没问题(我可以在我的树枝中显示“这些是发送的电子邮件”,随后列表)。唯一的问题是在登录页面中我可以点击刷新按钮,重新提交表单。因此,正如Symfony文档中所建议的那样,最好在form-> isValid()的末尾进行重定向。重定向的问题在于它通过Symfony的路由系统:我可以通过一些变量做类似的事情

return $this->redirect($this->generateUrl('name_of_the_route', array('param1' => $param1Value, 'param2' => $param2Value, )));

And that would generate the url with all the parameters (and values) in it.

这将生成包含所有参数(和值)的url。

But AFAIK I don't have the possibility of passing an array like the one I need (maybe containing 100 emails).

但是AFAIK我没有可能像我需要的那样传递数组(可能包含100封电子邮件)。

I was thinking about using a flash message and create a mega string with all the emails, comma separated. It's far from elegant (and is there a limit to the length of the flash message?).

我正在考虑使用flash消息并创建一个包含所有电子邮件的超级字符串,以逗号分隔。它远非优雅(闪存消息的长度有限制吗?)。

Any other solution?

还有其他方法吗?

Thank you!

1 个解决方案

#1


To get around the "can't pass an array" problem you could concatenate the email addresses into a single string delimited by something that doesn't occur in an email address. Maybe a pipe character |.

要解决“无法传递数组”问题,您可以将电子邮件地址连接到由电子邮件地址中不存在的内容分隔的单个字符串中。也许管道人物|。

The bigger issue you have is that 100 emails joined together is probably too much to comfortably put in the query string. You should probably save them on the server somewhere. Two ideas come to mind.

您遇到的更大问题是,连接在一起的100封电子邮件可能太多而无法轻松放入查询字符串中。您应该将它们保存在服务器的某个地方。想到两个想法。

  • Save the array in session. That's the quick and simple way and would probably work okay although session is not really intended for lots of data.
  • 将数组保存在会话中。这是快速而简单的方法,虽然会话并非真正用于大量数据,但可能会正常工作。

  • Save in a database. The simplest way I can think of is to have a table with an id and a big text or blob column. Serialize the array of email addresses, save them in the text column and redirect. You could use a Doctrine entity instead of the database directly. You have several choices of how to pass the id to the redirected action. Passing it as a single query parameter would work but if you do that and it's a simple integer id then users will be able to change the number and see the lists for other users. You could fix that by adding a random string or number in another column and pass that as another query parameter. The query in the redirected action then uses both to retrieve the list.
  • 保存在数据库中。我能想到的最简单的方法是拥有一个带有id和大文本或blob列的表。序列化电子邮件地址数组,将其保存在文本列中并重定向。您可以直接使用Doctrine实体而不是数据库。您可以选择如何将ID传递给重定向的操作。将其作为单个查询参数传递将起作用,但如果您这样做并且它是一个简单的整数id,则用户将能够更改该数字并查看其他用户的列表。您可以通过在另一列中添加随机字符串或数字并将其作为另一个查询参数传递来解决此问题。然后,重定向操作中的查询使用两者来检索列表。

#1


To get around the "can't pass an array" problem you could concatenate the email addresses into a single string delimited by something that doesn't occur in an email address. Maybe a pipe character |.

要解决“无法传递数组”问题,您可以将电子邮件地址连接到由电子邮件地址中不存在的内容分隔的单个字符串中。也许管道人物|。

The bigger issue you have is that 100 emails joined together is probably too much to comfortably put in the query string. You should probably save them on the server somewhere. Two ideas come to mind.

您遇到的更大问题是,连接在一起的100封电子邮件可能太多而无法轻松放入查询字符串中。您应该将它们保存在服务器的某个地方。想到两个想法。

  • Save the array in session. That's the quick and simple way and would probably work okay although session is not really intended for lots of data.
  • 将数组保存在会话中。这是快速而简单的方法,虽然会话并非真正用于大量数据,但可能会正常工作。

  • Save in a database. The simplest way I can think of is to have a table with an id and a big text or blob column. Serialize the array of email addresses, save them in the text column and redirect. You could use a Doctrine entity instead of the database directly. You have several choices of how to pass the id to the redirected action. Passing it as a single query parameter would work but if you do that and it's a simple integer id then users will be able to change the number and see the lists for other users. You could fix that by adding a random string or number in another column and pass that as another query parameter. The query in the redirected action then uses both to retrieve the list.
  • 保存在数据库中。我能想到的最简单的方法是拥有一个带有id和大文本或blob列的表。序列化电子邮件地址数组,将其保存在文本列中并重定向。您可以直接使用Doctrine实体而不是数据库。您可以选择如何将ID传递给重定向的操作。将其作为单个查询参数传递将起作用,但如果您这样做并且它是一个简单的整数id,则用户将能够更改该数字并查看其他用户的列表。您可以通过在另一列中添加随机字符串或数字并将其作为另一个查询参数传递来解决此问题。然后,重定向操作中的查询使用两者来检索列表。