【问题标题】:Render an array of input using Zend_Form使用 Zend_Form 渲染输入数组
【发布时间】:2011-06-17 20:10:20
【问题描述】:

如何构建一个这样呈现的zend表单

注意 child_name[] 作为最后 3 个输入标签上的名称。

(我故意省略了装饰器标签)

<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>

【问题讨论】:

    标签: zend-framework zend-form zend-form-element


    【解决方案1】:

    这是表单的代码

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

    然后在您的模板中回显表单。

    【讨论】:

    • 难道不能把所有的 child_name 元素都放到一个子表单中吗?
    猜你喜欢
    • 2015-09-26
    • 2010-11-29
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 2017-12-02
    相关资源
    最近更新 更多