views:

33

answers:

1

I have a single Zend_Form_Element_File element that handles the upload just fine. What I want to know is, is it possible to use the Zend_Form_Element_File element only and still maintain the value that was originally uploaded? Generally this sort of thing would be handled via a hidden field but I would like to keep it in the Zend_Form_Element_File element itself.

So, how I'd like to do it:

  1. Select a file (foo.txt)
  2. Upload the file with the destination hardcoded
  3. Store the filename in the database
  4. Reload the file but pre-fill the file element (?) with the stored value in the database (foo.txt)
  5. Pass that value back in when submitted (or read a newly submitted value (bar.txt)

I've tried overriding the setValue() method (grasping at straws), adding a custom decorator that put a hidden element alongside the file element, and nixing all of the other decorators and basically changing the field to a hidden field.

I've not super good luck with any because of frustration with the constraints.

So, is it possible to do something like this? What is the best way to do this (aside from toggling between either having a file element or having an empty element)?

Thank you.

+3  A: 

You can't meaningfully pre-populate an HTML file input, because you'll never know the location of the file on the user's local disk. That's by design.

If you've already got the file uploaded, and are redisplaying the form for whatever reason (because some other field failed to validate, or the user is editing the record later), you don't want to pre-populate the file input. You'll want to display (read-only) some information about the file (it's filename, or if it's an image, maybe a thumbnail), and provide and empty file input that the user can, optionally, use to replace the file.

timdev
@timdev: Yes, that is the best-practice. Do you have any knowledge on how this could be done within a single Zend Form element (e.g. Decorator, for example)?
Inkspeak
You might be able to use a Decorator, simply extend Zend_Form_Element_File into some kind of more application-specific element.
timdev