zend-framework - 使用 Zend_Form 渲染输入数组

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

如何构建一个像这样呈现的 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>

最佳答案

这是表单的代码

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

然后在您的模板中只需回显表单即可。

关于zend-framework - 使用 Zend_Form 渲染输入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6391250/

相关文章:

zend-framework - Zend_Form_Element_File 在不需要时在验证时返回 false

php - 如何禁用特定模块的错误 Controller

php - 如何在不触发错误的情况下检测类是否不存在

validation - Zend Framework Validate 字段是 1 到 5 之间的整数

zend-framework - 我将如何格式化 Zend_Form_Element_Radio 以便标签跟随输入?

html - 如何从内容中单独打印组显示?

php - 使 zend EmailAddress 表单验证器仅返回一条自定义错误消息

zend-framework - PHPUnit Zend_Test_PHPUnit_DatabaseTestCase 无法截断表

zend-framework - Zend 框架 : Add form in layout

zend-framework - Zend Form Element with Javascript - 装饰器、 View 助手或 View 脚本?