Zend_From的问题。无法设置Attribs,包括Method

时间:2011-01-26 22:11:58

标签: php zend-framework zend-form

我在添加方法和操作时遇到了一些困难,并让它们显示出来。

我正在创建一个表单,然后抓取一些关于应该从数据库进入的字段的信息,然后将它们添加到表单中,然后渲染它。这一切都完美无缺,我想提交表格,因为没有方法或行动。

这是代码,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中创建一个表单并在那里添加字段,它会呈现正常。如果我将表单传递给模型和后面,方法和操作就会消失,即使之前它已存在。

我将尝试在它自己的文件中创建一个新表单,动态添加表单文件中的字段,然后将其取回。 的 [EDIT2] 刚发现别的东西,这很奇怪。 当我从控制器直接回显表格时,它工作得很好。如果我将它分配给一个视图变量,然后在视图中回显它,它就不起作用。

2 个答案:

答案 0 :(得分:1)

尝试并更改

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

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

在您的视图中,只需执行此操作

echo $this->form
对于表单,

render()应该以Documented的形式传递给我,我从未使用setView()作为表单,并且它始终正常工作。

echo对象上调用print()Zend_Form将使用其__toString()方法并输出其生成的HTML。

希望有所帮助

答案 1 :(得分:0)

问题在于javascript干扰了这个过程。