Zend 1.12中的自定义表单

时间:2016-08-05 13:39:29

标签: php zend-framework zend-form

我创建了一个扩展Zend_Form的新类,但是当我尝试遍历新自定义表单中的所有元素(或者只计算所有元素)时,结果始终为0.

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 个答案:

答案 0 :(得分:1)

在迭代表单元素之前,您需要在parent::init()中调用Poroform_Form::init()

如果您按照流程进行操作,您会在Poroform_form::init()中看到在迭代表单元素的位置,您确实还没有添加任何内容。所有元素都添加在父Application_Form_ZendForm中。