2011-06-17 103 views
2

如何建立呈現這樣渲染Zend_Form的使用

通知child_name[]爲去年3個輸入標籤名稱Zend的形式輸入的數組。

(我中省略的裝飾標籤故意)

<form method="post" action="" enctype="application/x-www-form-urlencoded"> 
    <input type="text" value="" id="my_name" name="my_name"> 
    <input type="text" value="" id="wife_name" name="wife_name"> 
    <input type="text" value="" id="child_name-1" name="child_name[]"> 
    <input type="text" value="" id="child_name-2" name="child_name[]"> 
    <input type="text" value="" id="child_name-3" name="child_name[]"> 
</form> 

回答

4

這裏是形式

class MyForm extends Zend_Form 
{ 
    public function init() 
    { 
     $this->setMethod('post'); 
     $this->setEnctype('application/x-www-form-urlencoded'); 


     $my_name = $this->addElement('text', 'my_name'); 
     $wife_name = $this->addElement('text', 'wife_name'); 
     $child_name1 = $this->addElement('text', "child_name", array(
      'attribs' => array(
       'id' => 'child-name-1' 
      ), 
      'isArray' => true, 
     )); 

     $subForm = new Zend_Form(array('disableLoadDefaultDecorators' => true)); 

     $subForm->setDecorators(array(
      'FormElements', 
     )); 

     $subForm->addElement('text', 'child_name', array(
      'isArray' => true, 
      'decorators' => Array(
       'ViewHelper', 
      ), 
      'attribs' => array(
       'id' => 'child-name-2' 
      ), 
     )); 

     $subForm2 = new Zend_Form(array('disableLoadDefaultDecorators' => true)); 

     $subForm2->setDecorators(array(
      'FormElements', 
     )); 

     $subForm2->addElement('text', 'child_name', array(
      'isArray' => true, 
      'decorators' => Array(
       'ViewHelper', 
      ), 
      'attribs' => array(
       'id' => 'child-name-3' 
      ), 
     )); 

     $this->addSubForm($subForm, 'subform'); 
     $this->addSubForm($subForm2, 'subform2'); 

     $this->addDecorator('FormElements') 
      ->addDecorator('Form'); 


    } 

    /** 
    * Add Element to form without default decorators 
    * 
    * @see Zend_Form::addElement() 
    */ 
    public function addElement($element, $name = null, $options = null) 
    { 
     parent::addElement($element, $name, $options); 

     if (isset($this->_elements[$name])) { 
      $this->removeDecorators($this->_elements[$name]); 
     } 
    } 

    /** 
    * Create form element without default decorators 
    * 
    * @see Zend_Form::createElement() 
    */ 
    public function createElement($type, $name, $options = null) 
    { 
     $element = parent::createElement($type, $name, $options); 
     $this->removeDecorators($element); 
     return $element; 
    } 
    /** 
    * Remove default decorators for $element 
    * 
    * @param Zend_Form_Element $element 
    */ 
    protected function removeDecorators($element) 
    { 
     $element->removeDecorator('Label'); 
     $element->removeDecorator('HtmlTag'); 
     $element->removeDecorator('DtDdWrapper'); 
     $element->removeDecorator('Description'); 
    } 
} 

然後在你的模板的代碼只是呼應的形式。

+0

是不是所有的child_name元素可能只有一個子表單? – artsylar