PHP Zend Subform - view script decorator on subform element

282 Views Asked by At

I have a products form (stick with me here) which has a sub-form which is identified by the product array index and each of those sub-forms contains another sub-form (subformception) which is identified by the product ID and this sub-form contains a multi checkbox which displays each product two options: select the product and mark it as free.

When I add any sort of decorator (ideally I want to add a custom view script) nothing is output (with no errors). When I don't specify a decorator for the element it outputs the form.

The layout is below.

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

Here is my form. Is it possible to implement this way?

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

}

Thanks in advance :) Nathan

1

There are 1 best solutions below

0
On

The syntax of my decorator was slightly wrong and was short of one outer array. Below is how the decorator should be:

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