2010-07-25 11 views
0

我有一個Zend的形式是這樣的:如何preopulate Zend的文件元素

 $this->setName('Add Job'); 
    $id = new Zend_Form_Element_Hidden('id'); 
    $id->addFilter('Int'); 

    $name = new Zend_Form_Element_Text('name'); 
    $name->setLabel('Name') 
    ->setRequired(true) 
    ->addFilter('StripTags') 
    ->addFilter('StringTrim') 
    ->addValidator('NotEmpty'); 

    $file = new Zend_Form_Element_File('file'); 
    $file->setLabel('File') 
    ->setRequired(true); 

    $category = new Zend_Form_Element_Checkbox('category'); 
    $category->setLabel('Express?') 
    ->setRequired(true) 
    ->setCheckedValue('2') 
    ->setUncheckedValue('1'); 

    $submit = new Zend_Form_Element_Submit('submit'); 
    $submit->setAttrib('id', 'submitbutton'); 

「添加」行動工作正常,但我不能在「編輯」行爲在我的控制器使用這方面的工作:

$id = $this->_getParam('id', 0);  
if ($id > 0) { 
     $jobs = new Application_Model_DbTable_Jobs(); 
     $form->populate($jobs->getJob($id)); 
} 

和表單預填充除了文件元素之外還好。在數據庫中,我已經保存了文件名,我想以某種方式在編輯表單中顯示它 - 在Zend中是否有一種標準的處理方式?

感謝,

菲爾

回答

6

不能預填充文件輸入元素。
這不是Zend_Form的限制。 html input-file-element沒有「value」屬性。

+0

是的,完全正確。謝謝! – phil 2010-07-27 03:39:35

0

我使用了一個技巧,將文件名傳遞給視圖,例如可以在上傳旁邊顯示圖像。在您的形式輸入:

$this->getElement('image')->setAttrib('class', $this->_myParams['data']['image']); 
$image->setDecorators(array(
      'File', 
      array('ViewScript', array('viewScript' => '/ditta/_image.phtml', 'placement' => false))) 
     );/ 

然後在viewscript使用

$image = $this->element->getAttrib('class');//get name of file 
$this->element->setAttrib('class', '');//delete class