Zend 1.12 中的自定义表单

Custom form in Zend 1.12

我创建了扩展 Zend_Form 的新 class,但是当我尝试遍历我的新自定义表单中的所有元素(或仅计算所有元素)时,结果始终为 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"); 
        }
    }
}

那么我是不是在找自定义表格的时候弄错了?

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

如果按照流程进行操作,您会发现在 Poroform_form::init() 中迭代表单元素时,您确实还没有添加任何元素。所有元素都添加到父级 Application_Form_ZendForm.