I'm noticing what I think is a bug with Zend_Form_Element_File. Can others please test it and tell me if they get the same result. On a failed form, all fields re-populate except the value of Element_File.
My form has 2 text fields and 1 file field and all are required.
$name = new Zend_Form_Element_Text('name');
$name->setLabel('Name');
$name->setRequired(true);
$this->addElement($name);
$name2 = new Zend_Form_Element_Text('name2');
$name2->setLabel('Name2');
$name2->setRequired(true);
$this->addElement($name2);
$file = new Zend_Form_Element_File('file');
$file->setLabel('Test file')
->setDestination(APPLICATION_PATH);
$file->setRequired(true);
$this->addElement($file);
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Submit')
$this->addElement($submit);
Normally when a form fails, it automatically populates whatever fields were filled out, but it doesn't work for the file field, and I have to manually select the file again, in addition to correcting the other fields that need correcting.
First of all, could others confirm this?
I think the problem is related to the markup of the file field. I'm seeing that the first <input>
is the file size and then the file itself comes in the second <input>
.
<dd>
<input type="hidden" id="MAX_FILE_SIZE" value="67108864" name="MAX_FILE_SIZE">
<input type="file" id="file" name="file">
</dd>
and when a valid form is submitted, the post data shows only the first <input>
of the file field which is the size [MAX_FILE_SIZE] => 67108864
and not the file itself.
Array (
[name] => test
[MAX_FILE_SIZE] => 67108864
)
When the form is valid, the upload works fine so I know nothing's wrong with the file field itself, but in the case of a failed form (correct file field but one of the other fields is invalid), the file field is the only field that doesn't get re-populated when the form renders.开发者_如何学C So is this a bug? It's annoying to have to re-add the file when there was nothing wrong with it. Is there a workaround that someone can think of. I tried doing a manual populate in case of invalid form, hoping that it would force the file field to populate, something like this, but it also did not work.
if(//valid form){
//process the form
} else {
$form->populate($this->_request->getPost());
}
This isn't a bug with Zend_Form
. It's a limitation HTMLs file upload input element. Browsers will ignore and value set on a file input for security reasons. If this limitation wasn't in place, malicious websites could pre-populate the file input with things like
/etc/password
c:\Windows\Location\Of\Sensative\File.txt
and then auto post the contents of your hard drive to any server.
One common work around is to use something like the ajax form plugin for jQuery to post the upload via an ajax request. When it fails, the file upload stays selected, as there's no page refresh.
it is no a bug. Some Zend_Form elements has not repopulate functionality. This made for some specific elements (password, file, ...). I think that it is correct.
精彩评论