簡體   English   中英

Zend_From問題。 無法設置屬性,包括方法

[英]Problem with Zend_From. Not able to set Attribs, including Method

我在添加方法和操作以及使其顯示時遇到一些困難。

我正在創建一個表單,然后從數據庫中獲取一些應該進入表單的字段信息,然后將它們添加到表單中,然后進行渲染。 由於沒有方法或動作,因此一切都可以完美完成,直到我要提交表單為止。

這是代碼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();
    }

救命?!

[編輯]剛剛發現了一些奇怪的東西。 如果我在Controller內創建一個表單並在其中添加字段,則可以正常顯示。 如果我將Form傳遞給Model和背面,那么方法和動作就不存在了,即使以前也沒有。

我將嘗試在其自己的文件中創建一個新表單,在表單文件中動態添加字段,然后將其取回。 [EDIT2]剛剛發現了其他東西,這很奇怪。 當我直接從控制器回顯表單時,它就可以正常工作。 如果我將其分配給視圖變量,然后在視圖中回顯它,則它將不起作用。

嘗試改變

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

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

然后在您看來

echo $this->form

應該將傳遞給表單的render()傳遞為Documented的View,我從未將setView()用於表單,並且它總是可以正常工作。

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

希望能有所幫助

問題出在使用JavaScript干擾了該過程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM