2016-08-05 65 views
1

我創建了擴展Zend_Form的新類,但是當我正嘗試通過我的新的自定義表單所有元素迭代(或僅僅指望他們全部)的結果始終爲0。自定義表單1.12

class Application_Form_ZendForm extends Poroform_Form { 
    public function init() { 
     parent::init(); 

     $this->setAttrib('id', 'contact-form'); 

     $this->addElement('text', 'textt', array(
      'label' => 'Foo', 
      'value' => 'test', 
      "attribs" => array(
       "icon" => "icon-append fa fa-user", 
       "section" => "col col-6" 
      ), 
     )); 
    } 
} 


class Poroform_Form extends Zend_Form { 

    public function __construct($options = null) { 
     parent::__construct($options); 
    } 

    public function init() { 
     $this->setAttrib("class", "smart-form"); 
     $this->setAttrib("novalidate", "novalidate"); 
     $this->setAttrib("elem_count", count($this->getElements())); //why is result 0? 

     $this->addElementPrefixPath('Poroform_Form_Decorator_', '/Poroform/Form/Decorator/', 'decorator'); 
     $this->setElementDecorators(Array('SmartadminInput')); 

     foreach ($this->getElements() as $element) { //not working 
       $element->setAttrib("test", "aaa"); 
     } 
    } 
} 

所以我在找錯方的自定義表單?

回答

1

你需要調用parent::init()Poroform_Form::init()您遍歷窗體元素之前。

如果你按照這個流程,你會發現在Poroform_form::init()上你迭代表單元素的時候,你還沒有添加任何表單。所有元素都添加到父項Application_Form_ZendForm中。

+1

Thx for replay David。在'Poroform_Form :: init()'的開頭添加'parent :: init()'並不能解決問題。實際上,我通過在'Poroform_Form :: __ construct()'開頭添加'$ this-> init();'來解決它。 – porosman