1

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

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

布局如下。

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

}

在此先感谢:) 内森

4

1 回答 1

1

我的装饰器的语法稍有错误,并且缺少一个外部数组。下面是装饰器应该如何:

'decorators' => array(
    array(
        'ViewScript', array(
            'viewScript' => '/admin/partials/property-products.phtml',
            'category' => 'services',
            'options' => $product
        )
     )
)
于 2015-01-12T14:36:49.863 回答