2011-01-26 67 views
0

我在添加方法和操作時遇到了一些困難,並讓它們顯示。Zend_From有問題。無法設置屬性,包括方法

我創建了一個表單,然後從數據庫中獲取關於字段的一些信息,然後將它們添加到表單中,然後將其呈現。它完全符合我的觀點,我希望提交表格,因爲沒有任何方法或行動。

下面的代碼,後續代碼var_dump和生成的HTML:

public function getForm($id) { 
     $ar= $this->formElementModel->getElements($id); 
     //print_r($ar); 
     $form= new Zend_Form(); 

     $form->setAction('/resource/process') 

      ->setMethod('post'); 

       foreach ($ar as $arr) { 
      $form->addElement($this->elementModel->getElement($arr['formElementId'], $this->genRandomString(),false)); 
      //$fieldArray[] = ; 

     } 

     $form->addElement('submit', 'submit',array('order'=>100))->setMethod('post'); 
     var_dump($form); 
     // var_dump($fieldArray); 
     return $form; 
    } 

的的var_dump()輸出:

object(Zend_Form)[97] 
    protected '_attribs' => 
    array 
     'action' => string '/resource/process' (length=17) 
     'method' => string 'post' (length=4) 
    protected '_decorators' => 
    array 
     'FormElements' => 
     array 
      'decorator' => string 'FormElements' (length=12) 
      'options' => null 
     'HtmlTag' => 
     array 
      'decorator' => string 'HtmlTag' (length=7) 
      'options' => 
      array 
       ... 
     'Form' => 
     array 
      'decorator' => string 'Form' (length=4) 
      'options' => null 
    protected '_defaultDisplayGroupClass' => string 'Zend_Form_DisplayGroup' (length=22) 
    protected '_description' => null 
    protected '_disableLoadDefaultDecorators' => boolean false 
    protected '_displayGroupPrefixPaths' => 
    array 
     empty 
    protected '_displayGroups' => 
    array 
     empty 
    protected '_elementDecorators' => null 
    protected '_elementPrefixPaths' => 
    array 
     empty 
    protected '_elements' => 
    array 
     'textElement40_yu75r8x22i' => 
     object(Zend_Form_Element_Text)[106] 
      public 'helper' => string 'formText' (length=8) 
      protected '_allowEmpty' => boolean true 
      protected '_autoInsertNotEmptyValidator' => boolean true 
      protected '_belongsTo' => null 
      protected '_decorators' => 
      array 
       ... 
      protected '_description' => null 
      protected '_disableLoadDefaultDecorators' => boolean false 
      protected '_errorMessages' => 
      array 
       ... 
      protected '_errors' => 
      array 
       ... 
      protected '_errorMessageSeparator' => string '; ' (length=2) 
      protected '_filters' => 
      array 
       ... 
      protected '_ignore' => boolean false 
      protected '_isArray' => boolean false 
      protected '_isError' => boolean false 
      protected '_isErrorForced' => boolean false 
      protected '_label' => string 'Teksta elements' (length=15) 
      protected '_loaders' => 
      array 
       ... 
      protected '_messages' => 
      array 
       ... 
      protected '_name' => string 'textElement40_yu75r8x22i' (length=24) 
      protected '_order' => null 
      protected '_required' => boolean false 
      protected '_translator' => null 
      protected '_translatorDisabled' => boolean false 
      protected '_type' => null 
      protected '_validators' => 
      array 
       ... 
      protected '_validatorRules' => 
      array 
       ... 
      protected '_value' => null 
      protected '_view' => null 
      protected '_isPartialRendering' => boolean false 
     'selectElement39_19voehc8bz' => 
     object(Zend_Form_Element_Select)[101] 
      public 'helper' => string 'formSelect' (length=10) 
      public 'options' => 
      array 
       ... 
      protected '_registerInArrayValidator' => boolean true 
      protected '_separator' => string '<br />' (length=6) 
      protected '_translated' => 
      array 
       ... 
      protected '_allowEmpty' => boolean true 
      protected '_autoInsertNotEmptyValidator' => boolean true 
      protected '_belongsTo' => null 
      protected '_decorators' => 
      array 
       ... 
      protected '_description' => string 'Vienlkāršs elementsa' (length=22) 
      protected '_disableLoadDefaultDecorators' => boolean false 
      protected '_errorMessages' => 
      array 
       ... 
      protected '_errors' => 
      array 
       ... 
      protected '_errorMessageSeparator' => string '; ' (length=2) 
      protected '_filters' => 
      array 
       ... 
      protected '_ignore' => boolean false 
      protected '_isArray' => boolean false 
      protected '_isError' => boolean false 
      protected '_isErrorForced' => boolean false 
      protected '_label' => string 'Dynamic elements' (length=16) 
      protected '_loaders' => 
      array 
       ... 
      protected '_messages' => 
      array 
       ... 
      protected '_name' => string 'selectElement39_19voehc8bz' (length=26) 
      protected '_order' => null 
      protected '_required' => boolean false 
      protected '_translator' => null 
      protected '_translatorDisabled' => boolean false 
      protected '_type' => null 
      protected '_validators' => 
      array 
       ... 
      protected '_validatorRules' => 
      array 
       ... 
      protected '_value' => null 
      protected '_view' => null 
      protected '_isPartialRendering' => boolean false 
      public 'escape' => boolean false 
     'textElement37_wz47pigru9' => 
     object(Zend_Form_Element_Text)[109] 
      public 'helper' => string 'formText' (length=8) 
      protected '_allowEmpty' => boolean true 
      protected '_autoInsertNotEmptyValidator' => boolean true 
      protected '_belongsTo' => null 
      protected '_decorators' => 
      array 
       ... 
      protected '_description' => null 
      protected '_disableLoadDefaultDecorators' => boolean false 
      protected '_errorMessages' => 
      array 
       ... 
      protected '_errors' => 
      array 
       ... 
      protected '_errorMessageSeparator' => string '; ' (length=2) 
      protected '_filters' => 
      array 
       ... 
      protected '_ignore' => boolean false 
      protected '_isArray' => boolean false 
      protected '_isError' => boolean false 
      protected '_isErrorForced' => boolean false 
      protected '_label' => string 'Nosaukums' (length=9) 
      protected '_loaders' => 
      array 
       ... 
      protected '_messages' => 
      array 
       ... 
      protected '_name' => string 'textElement37_wz47pigru9' (length=24) 
      protected '_order' => null 
      protected '_required' => boolean false 
      protected '_translator' => null 
      protected '_translatorDisabled' => boolean false 
      protected '_type' => null 
      protected '_validators' => 
      array 
       ... 
      protected '_validatorRules' => 
      array 
       ... 
      protected '_value' => null 
      protected '_view' => null 
      protected '_isPartialRendering' => boolean false 
     'submit' => 
     object(Zend_Form_Element_Submit)[98] 
      public 'helper' => string 'formSubmit' (length=10) 
      protected '_allowEmpty' => boolean true 
      protected '_autoInsertNotEmptyValidator' => boolean true 
      protected '_belongsTo' => null 
      protected '_decorators' => 
      array 
       ... 
      protected '_description' => null 
      protected '_disableLoadDefaultDecorators' => boolean false 
      protected '_errorMessages' => 
      array 
       ... 
      protected '_errors' => 
      array 
       ... 
      protected '_errorMessageSeparator' => string '; ' (length=2) 
      protected '_filters' => 
      array 
       ... 
      protected '_ignore' => boolean true 
      protected '_isArray' => boolean false 
      protected '_isError' => boolean false 
      protected '_isErrorForced' => boolean false 
      protected '_label' => null 
      protected '_loaders' => 
      array 
       ... 
      protected '_messages' => 
      array 
       ... 
      protected '_name' => string 'submit' (length=6) 
      protected '_order' => int 100 
      protected '_required' => boolean false 
      protected '_translator' => null 
      protected '_translatorDisabled' => boolean false 
      protected '_type' => null 
      protected '_validators' => 
      array 
       ... 
      protected '_validatorRules' => 
      array 
       ... 
      protected '_value' => null 
      protected '_view' => null 
      protected '_isPartialRendering' => boolean false 
    protected '_elementsBelongTo' => null 
    protected '_errorMessages' => 
    array 
     empty 
    protected '_errorsExist' => boolean false 
    protected '_errorsForced' => boolean false 
    protected '_formOrder' => null 
    protected '_isArray' => boolean false 
    protected '_legend' => null 
    protected '_loaders' => 
    array 
     'DECORATOR' => 
     object(Zend_Loader_PluginLoader)[107] 
      protected '_loadedPluginPaths' => 
      array 
       ... 
      protected '_loadedPlugins' => 
      array 
       ... 
      protected '_prefixToPaths' => 
      array 
       ... 
      protected '_useStaticRegistry' => null 
     'ELEMENT' => 
     object(Zend_Loader_PluginLoader)[111] 
      protected '_loadedPluginPaths' => 
      array 
       ... 
      protected '_loadedPlugins' => 
      array 
       ... 
      protected '_prefixToPaths' => 
      array 
       ... 
      protected '_useStaticRegistry' => null 
    protected '_methods' => 
    array 
     0 => string 'delete' (length=6) 
     1 => string 'get' (length=3) 
     2 => string 'post' (length=4) 
     3 => string 'put' (length=3) 
    protected '_order' => 
    array 
     'textElement40_yu75r8x22i' => null 
     'selectElement39_19voehc8bz' => null 
     'textElement37_wz47pigru9' => null 
     'submit' => int 100 
    protected '_orderUpdated' => boolean true 
    protected '_subFormPrefixPaths' => 
    array 
     empty 
    protected '_subForms' => 
    array 
     empty 
    protected '_translator' => null 
    protected '_translatorDisabled' => boolean false 
    protected '_view' => null 
    protected '_isRendered' => boolean false 

並將所生成的HTML:

<form id="fZvN9un1xn4Uh9yZzgWOi9NluFf6CiOuFWH6ugVQSkapm2SV2Jb"> 
<dl class="zend_form"> 
<dt id="textElement40_yu75r8x22i-label"><label for="textElement40_yu75r8x22i" class="optional">Teksta elements</label></dt> 
<dd id="textElement40_yu75r8x22i-element"> 
<input name="textElement40_yu75r8x22i" id="textElement40_yu75r8x22i" value="" type="text"></dd> 
<dt id="selectElement39_19voehc8bz-label"><label for="selectElement39_19voehc8bz" class="optional">Dynamic elements</label></dt> 
<dd id="selectElement39_19voehc8bz-element"> 
<select name="selectElement39_19voehc8bz" id="selectElement39_19voehc8bz"> 
    <option value="sdffdsf" label="Nekāda sudfdfdss s s nebūs! - newText26">Nekāda sudfdfdss s s nebūs! - newText26</option> 

    <option value="newName21" label="newTitle22 - newText23">newTitle22 - newText23</option> 
</select> 
<p class="description">Vienlkāršs elementsa</p></dd> 
<dt id="textElement37_wz47pigru9-label"><label for="textElement37_wz47pigru9" class="optional">Nosaukums</label></dt> 
<dd id="textElement37_wz47pigru9-element"> 
<input name="textElement37_wz47pigru9" id="textElement37_wz47pigru9" value="" type="text"></dd> 
<dt id="submit-label">&nbsp;</dt><dd id="submit-element"> 
<input name="submit" id="submit" value="submit" type="submit"></dd></dl></form> 

控制器腳本

public function viewAction($formId=NULL) { 
     $id = $formId; 

     if ($formId === NULL) { 
      $id = $this->_getParam('id'); 
     } 
     $form=$this->formModel->getForm($id); 

     if ($this->getRequest()->isPost()) { 
print_r($this->_getAllParams()); 
     } 
     $this->view->fields =$form ->render(); 
    } 

幫助?

[編輯] 剛剛發現了一些奇怪的東西。 如果我在控制器中創建一個表單並在其中添加字段,則呈現正常。如果我將表格傳遞給模型和後面,那麼方法和行爲就會消失,儘管它之前就在那裏。

我會嘗試在自己的文件中創建一個新窗體,動態添加窗體文件中的字段,然後將其取回。 [編輯2] 剛剛發現了別的東西,這很奇怪。 當我直接從控制器回顯表單時,它工作得很好。如果將其分配給視圖變量,然後在視圖中回顯它,則不起作用。

+0

你如何輸出`$ form`?編輯了 – 2011-01-26 23:09:31

+0

。我在控制器中渲染()它,然後在視圖中回顯()。 – 2011-01-26 23:18:35

回答

1

嘗試改變

$this->view->fields =$form ->render(); 

$this->view->form =$form; 

並且在視圖只是這樣做

echo $this->form 

render()的形式應傳遞一個查看Documented,我有從來沒有使用過setView()作爲表單,它總是正常工作。

調用echoprint()對一個Zend_Form對象將使用其__toString()方法並輸出其生成的HTML。

希望可以幫到

0

問題是在JavaScript中干擾了這個過程。