dtra49684 2011-01-26 22:11
浏览 24

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

I'm having some difficulty adding the method and the action, and getting them to display.

I'm creating a form, then grabbing some info about fields that should go into it from a Database, and then adding them to the form, then rendering it. It all works perfectly up to the point, where I want to submit the form, since there is no method or action.

Here's the code, var_dump and generated 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;
    }

The var_dump() output:

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

and the generated 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>

The controller script

    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();
    }

Help?!

[EDIT] Just found out something weird. If I create a form inside the Controller and add the fields there, it renders ok. If I pass the Form to the Model and the back, the method and action is gone, even though, it was there before.

I shall try creating a new form in it's own file, dynamically add the fields in the Form file, and then get it back. [EDIT2] Just found out something else, that's weird. When I echo the form straight form the controller, it works just fine. If I assign it to a view variable, and then echo it in the view, it doesn't work.

  • 写回答

2条回答 默认 最新

  • doude2635 2011-01-26 23:25
    关注

    Try and change

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

    To

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

    And in your View just do this

    echo $this->form
    

    render() for a form should be passed a View as Documented, I have never used setView() for a form and its always worked fine.

    Calling echo or print() on a Zend_Form object will use its __toString() method and output its generated HTML.

    Hope that helps

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题