PHP Zend子表单 - 查看子表单元素上的脚本装饰器

时间:2015-01-12 13:03:07

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

我有一个产品表格(在这里贴我),它有一个由产品数组索引标识的子表单,每个子表单包含另一个子表单(子表单),由产品ID标识此子表单包含一个多重复选框,显示每个产品有两个选项:选择产品并将其标记为免费。

当我添加任何类型的装饰器(理想情况下我想添加自定义视图脚本)时,不会输出任何内容(没有错误)。当我没有为元素指定装饰器时,它会输出表单。

布局如下。

products sub-form [
    selection sub-form [
        MultiCheckbox element[
            decorator[
                ViewScript[]
            ]
        ]
    ] 
]

这是我的表格。是否有可能以这种方式实现?

<?php

/**
 * Properties_Form_Admin_Products
 */

/**
 * Admin form for creating a new property
 *
 * @category    Properties
 * @package     Form
 */
class Properties_Form_Admin_Products extends Cms_Form_DtDd {

    /**
     * @var Properties_Model_Property
     */
    protected $_property;

    /**
     * @var Properties_Manager_Property
     */
    protected $_propertyManager;

    /**
     * @var array
     */
    private $products;

    /**
     * Initialize form (extended from Zend_Form)
     *
     * @return void
     */
    public function init() {
        parent::init();

        $this->_propertyManager = Caboodle_Manager_Factory::get('Property');

        $request = Zend_Controller_Front::getInstance()->getRequest();
        $this->setMethod('post')
                ->setAttrib('id', 'product_form')
                ->setAttrib('class', 'page_form admin_form')
                ->setDecorators($this->formDecorators)
                ->setAction($this->getView()->url());

        $subform = $this->addSubform(new Zend_Form_SubForm, 'products')
            ->getSubform('products')
            ->clearDecorators()
            ->addDecorator('FormElements');

        // Add subform for each existing product time.
        foreach ($this->getProducts() as $index => $product) {
            $subform->addSubform(new Zend_Form_SubForm, (string) $index)
                    ->getSubform((string) $product->getId())
                    ->addElements(array(
                        new Zend_Form_Element_MultiCheckbox('selection', array(
                            'label' => $product->getName() . ' ('.$product->getDescription().')',
                            'decorators' => array(
                                // This form displays when the below decorator is commented out
                                array('ViewScript', array(
                                        'viewScript' => '/partials/property-products.phtml',
                                        'category' => 'Products',
                                        'options' => $product
                                    )
                                )
                            ),
                            'multiOptions' => array(
                                'select' => 'Select',
                                'free' => 'Mark as free'
                            )
                        ))
                    ));
        }

        /* buttons */
        $submit = new Zend_Form_Element_Submit('submit_btn');
        $submit->setRequired(false)
                ->setIgnore(true)
                ->setLabel('Add and Pay')
                ->setAttrib('class', 'pos_btn')
                ->setDecorators($this->buttonDecorators);
        $this->addElement($submit);

        $this->addDisplayGroup(
                array('submit_btn'), 'buttons', array('decorators' => $this->plainGroupDecorators)
        );
    }

    /**
     * Validate the form
     *
     * @param  array $data
     * @return boolean
     */
    public function isValid($data) {

        parent::isValid($data);

        return !$this->_errorsExist;
    }

    /**
     * Handle all of the form processing for the login form
     *
     * @param Zend_Controller_Request_Abstract $request
     * @return void
     */
    public function processForm(Zend_Controller_Request_Abstract $request) {
        if ($request->isPost()) {

            if ($this->isValid($request->getPost())) { // valid
                $values = $this->getValues();
            }
        }
    }

    /**
     * @param $products
     * @return $this
     */
    protected function setProducts($products)
    {
        $this->products = $products;
        return $this;
    }

    /**
     * @return array
     */
    public function getProducts()
    {
        return $this->products;
    }

}

提前致谢:) 森

1 个答案:

答案 0 :(得分:1)

我的装饰器的语法略有错误,缺少一个外部数组。以下是装饰者应该如何:

'decorators' => array(
    array(
        'ViewScript', array(
            'viewScript' => '/admin/partials/property-products.phtml',
            'category' => 'services',
            'options' => $product
        )
     )
)