2010-07-08 87 views
2

我創建了一個簡單的上傳表單,並在提交數據時遇到了一些問題。Zend表格文本值保留爲空

該文件上傳正確,但我的小描述字段保持爲空。

這裏是我的形式:

class Upload_Form_Uploadvideo extends Zend_Form{ 
public function init() 
{ 
    $this->setName('video') 
    ->setAction('interface/videoupload') 
    ->setMethod('post'); 


    #id 
    $id = new Zend_Form_Element_Hidden('id'); 
    $id->addFilter('Int'); 

    #Textfield "Videofile" 
    $video = new Zend_Form_Element_File('videofile', array(
     'label'   => 'Videofile') 
    ); 
    $video->setDestination(APPLICATION_PATH.'/upload/video/toConvert/'); 

    #Textfield "Videofile" 
    $desc = new Zend_Form_Element_Text('videodescription', array(
     'label'   => 'Description') 
    ); 
    $desc->setAttrib('value','The description is not optional!'); 
    $desc->setAttrib('size','25'); 


    #Submit 
    $submit = new Zend_Form_Element_Submit('submit', array('label' => 'Upload Video')); 
    $submit->setAttrib('id', 'submitbutton'); 


    #bringing everything together 
    $this->addElements(array($id,$video,$desc,$submit)); 

    } 
} 

控制器,給它的觀點:

public function videouploadAction() 
{ 
    #in production this code goes to the index() 
    if(!$this->getRequest()->isPost()){ 
     return $this->_forward('index'); 
    } 
    $form = $this->getForm(); 
    $this->view->via_getpost = var_dump($this->_request->getPost()); 

    $this->view->via_getvalues= var_dump($form->getValues()); 

} 

現在,我的var_dump $這個 - > _請求 - >的getPost()和$形式 - >的GetValues()。

的輸出是下面的:

array[$this->_request->getPost()] 

'id' => string '0' (length=1) 
'MAX_FILE_SIZE' => string '134217728' (length=9) 
'videodescription' => string 'This is a test-video' (length=20) 
'submit' => string 'Upload Video' (length=12) 


array [$form->getValues()] 

'id' => int 0 
'videofile' => string 'swipeall.avi' (length=12) 
'videodescription' => null 

此外,我設置 「值」 -attrib,無任何影響。當用戶加載站點時,我打算在框中寫一些東西。

我是新來的Zend,所以我想我只是在監督一些愚蠢的東西,儘管我找不到它。

更新:

我真的有通過

$formdata = $this->getRequest()->getPost(); 

回答

3

使用

$desc->setValue('The description is not optional!'); 

,而不是

$desc->setAttrib('value','The description is not optional!'); 
+0

呀拿到$ _ POST-數據,說了這麼用文本字段的預定義文本標記事物,但現在它始終使用我初始設置的值。 我不認爲,我必須採取我的文本字段的值,我想寫在數據庫中,出$ _POST或什麼(至少我不想這樣做) – 2010-07-08 07:07:37

+0

Zend_Form可以使用用於構建表單或驗證/獲取表單值。只有在生成表單時才需要使用setValue()。 爲什麼你不想使用$ _POST?你可以使用你的表單對象來驗證它:$ form-> isValid($ _ POST)。 (你必須首先添加一些驗證器到表單元素) – 2010-07-08 07:12:17