Symfony 表单 - 记住先前选择的文件

发布于 2024-08-31 04:47:36 字数 902 浏览 2 评论 0原文

我在 Ubuntu 上使用 Symfony 1.3.2,并且有一个包含多个小部件的表单。 sfWidgetFormInputFile 是一个小部件,它允许用户选择一张图片。

如果表单无效,我会再次将其呈现给用户,以更正错误的字段。

我遇到的问题是,目前,当 foem 无法验证(无论出于何种原因)时,用户指定的图片文件就会丢失(即他们必须再次选择该文件)。

如何在所选图片文件仍然存在的情况下再次向用户显示表单(这样他们就不必再次选择文件)?

在处理 POSTed 表单的操作中,我调用 getFiles($key) ,然后索引检索到的数组以获取文件信息,并设置小部件的默认值(见下文)。但是,渲染的小部件是空的(不保留先前选择的文件的路径名值。

我的(操作)代码片段如下所示:

$files = $request->getFiles('foobar');

if(!empty($files) && isset($files['pict_path'])){
   $pic_info = $files['pict_path'];
   $originalName =$pic_info['name'];
   $type = $pic_info['type'];
   $tempName = $pic_info['tmp_name'];
   $size = $pic_info['size'];
   $pict_file = new sfValidatedFile($originalName, $type, $tempName, $size);

   $this->form->setWidget('pict_path', new sfWidgetFormInputFile(array('default'=>$pict_file)));
}

任何人都可以建议如何正确实现上述所需的行为吗?

I am using Symfony 1.3.2 on Ubuntu and I have a form that contains several widgets. One widget is the sfWidgetFormInputFile, which allows a user to select a picture.

If the form is not valid, I present it again to the user, to correct the erroneous field(s).

The problem I am experiencing is that currently, when the foem fails to validate (for whatever reason), the picture file that was specified by the user is lost (i.e. they have to select the file again).

How can I show the form to the user again, with the selected picture file still there (so they dont have to select the file again)?

In my action that process the POSTed form, I call getFiles($key) and then index the retrieved array to get the file information, and setting the default value of the widget (see below). However, the rendered widget is empty (does not retain the value of the pathname to the previously selected file.

A snippet of my (action) code looks like this:

$files = $request->getFiles('foobar');

if(!empty($files) && isset($files['pict_path'])){
   $pic_info = $files['pict_path'];
   $originalName =$pic_info['name'];
   $type = $pic_info['type'];
   $tempName = $pic_info['tmp_name'];
   $size = $pic_info['size'];
   $pict_file = new sfValidatedFile($originalName, $type, $tempName, $size);

   $this->form->setWidget('pict_path', new sfWidgetFormInputFile(array('default'=>$pict_file)));
}

Can anyone suggest how to correctly implement the desired behavior described above?

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

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

发布评论

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

评论(2

作妖 2024-09-07 04:47:36

输入 html 标签(type=“file”)的 value 属性是只读的。
您无法将其设置为默认值。这将允许您通过设置输入标签的值将用户硬盘驱动器的任何文件上传到您的服务器。

用户每次提交表单时都需要隐式选择文件。
托马斯提出的解决方案是一个很好的解决方案。将文件存储到临时文件夹,将此信息存储在会话中(以便您可以在表单中的某个地方再次显示此信息),并在成功提交表单后清除文件的会话信息并将文件移动到正确的目的地。

请参阅本页 B.10.1 W3C 网站上表单的安全问题: http://www.w3.org/TR/html401/appendix/notes.html#forms-security

The value attribute of the input html tag (with type="file") is read-only.
You can't set it to a default value. This would allow you to upload any file of the users hard drive to your server by setting the value of the input tag.

The user needs to implicitly choose the file every time he/she submits the form.
The solution Tomas proposes is a good one. Store the file to a temp folder, store this information in the session (so you can show this in your form again somewhere) and after a succesfull form submittal clear the session info for the file and move the file to the right destination.

See this page, B.10.1 Security issues for forms on the W3C website: http://www.w3.org/TR/html401/appendix/notes.html#forms-security

只是一片海 2024-09-07 04:47:36

我认为您无法保存文件上传控件的状态。我认为你不应该这样做。

您可以在处理表单的其余部分之前上传文件。因此,您将该过程分为两部分:

  1. 文件验证
  2. 表单验证

如果表单验证失败并且用户没有尝试使用正确的信息重新提交它,您稍后将丢弃这些文件。在我看来,这增加了一些开销,但改善了用户体验。用户可能会错误地输入错误的数据,但当您使用适当的说明重新加载页面时,文件仍然存在。

I don't think you can save the state of a file upload control. And I don't think you should.

You could upload the files before the rest of the form is processed. Thus you split the process in two:

  1. File validation
  2. Form validation

If the form validation fails and the user does not attempt to re-submit it using correct information, you discard the files later. This adds some overhead but improves the user experience in my view. The user can mistakenly enter wrong data but as you reload the page with appropriate instructions, the files are still there.

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