views:

38

answers:

0

Hi all,

Trying to set up a form partly using subforms that i also use elsewhere.

I have setup the decorators to utilize UL-LI notation instead of the regular DtDd wrapper.

Structure is something like this.

RegisterForm
    + Element, firstname
    + Element, lastname
    + SubForm, NoScriptForm [new Olo_AddressApartmentForm()]
        + Elements are added within the form init()
    + SubForm, HouseForm [new Olo_AddressHouseForm()]
        + Elements are added within the form init()
    + SubForm, CellPhone [new UserForm()]
        + Element 'identity' is added to Login Subform from RegisterForm
    + Submit, element created in and added to RegisterForm

Heres the RegisterForm:

<?php

class UserRegisterForm extends IdentityForm
{
    public $locationType;

    public $noScriptForm;
    public $houseForm;

    public $cellphoneForm;

    public $submit;

    public function init()
    {
        parent::init();

        $this->setName('location-address-form');

        $this->locationType = $this->createElement('select', 'form_address_location_type', array('multiOptions' => array('delivery' => 'delivery', 'pickup' => 'pickup')));

        $this->noScriptForm = new Olo_AddressApartmentForm();        
        $this->houseForm = new Olo_AddressHouseForm();

        $this->houseForm->setOptions(array('id' => 'address-type-house-pane', 'class' => 'address-type-pane'));
        $this->houseForm->city->setRequired(true);

        $this->cellphoneForm = new UserForm();
        $this->cellphoneForm->addElement($this->cellphoneForm->identity);
//        $this->cellphoneForm->loadDefaultDecorators();

        $this->submit = $this->createElement('submit', 'form_address_location_submit');

        // Add elements and subforms
        $this->addElements(array($this->firstname, $this->lastname, $this->locationType));

        $this->addSubForms(array('noScriptForm' => $this->noScriptForm,
                                 'houseForm' => $this->houseForm,
                                 'cellphoneForm' => $this->cellphoneForm
                                )
                          );        

        $this->addElement($this->submit);                           

        // Control Decorators
        // Apply decorators now so we can change them later
        $this->loadDefaultDecorators();

        // Set decorators for the noScript form
        $liDecorator = $this->noScriptForm->getDecorator('Li');
        $this->noScriptForm->removeDecorator('Li');
        $this->noScriptForm->addDecorator(array('noScript' => 'HtmlTag'), array('tag' => 'noscript'));
        $this->noScriptForm->addDecorator($liDecorator);

        $this->submit->removeDecorator('Label');
    }
}

All subforms extends from My_Form_Subform, in the code above UserRegisterForm extends from IdentityForm, this form creates all elements matching my Identities model. It does not add them, thats to job of the forms that extend of it.

The loadDefaultDecorators() of My_Form_Subform:

public function loadDefaultDecorators()
{
    if ($this->loadDefaultDecoratorsIsDisabled()) {
        return;
    }

    $decorators = $this->getDecorators();
    if (empty($decorators)) {
        parent::loadDefaultDecorators();
        $this->setSubFormDecorators(array(
            'FormElements',
            array('decorator' => array('Ul' => 'HtmlTag'), 'options' => array('tag' => 'ul')),
            'Fieldset',        
            array('decorator' => array('Li' => 'HtmlTag'), 'options' => array('tag' => 'li')),            
        ));
    }
}

My_Form_Subform extend My_Form, which have the following loadDefaultDecorators()

public function loadDefaultDecorators($disableLoadDefaultDecorators = false)
{
    if ($this->loadDefaultDecoratorsIsDisabled()) {
        return;
    }

    $this->setDisableLoadDefaultDecorators($disableLoadDefaultDecorators);

    $decorators = $this->getDecorators();
    if (empty($decorators)) {
        $this->addDecorator('FormElements')
             ->addDecorator('HtmlTag', array('tag' => 'ul'))
             ->addDecorator('Form');
    }
    //Set the decorators we need:
    $this->setElementDecorators(array(
        'ViewHelper',
        array('decorator' => array('Div' => 'HtmlTag'), 'options' => array('tag' => 'div', 'class' => 'element')),
        'Label',        
        'Errors',
        array('decorator' => array('Li' => 'HtmlTag'), 'options' => array('tag' => 'li')),        
    ));

    //Set decorators for the displaygroup:
    $this->setDisplayGroupDecorators(array(
        'FormElements',
        array('decorator' => array('Ul' => 'HtmlTag'), 'options' => array('tag' => 'ul')),
        'Fieldset',        
        array('decorator' => array('Li' => 'HtmlTag'), 'options' => array('tag' => 'li')),            
    ));

    $this->setSubFormDecorators(array(
        'FormElements',
        array('decorator' => array('Ul' => 'HtmlTag'), 'options' => array('tag' => 'ul')),
        'Fieldset',        
        array('decorator' => array('Li' => 'HtmlTag'), 'options' => array('tag' => 'li')),            
    ));
}

And now to the problem at hand.

The decorators works fine on the elements (firstname, lastname) and the subforms noScriptForm and houseForm that i add, but the cellphoneForm (UserForm()) that i also add, shows the regular DtDd-wrapper, and not the Ul-Li notation like the others.

If i add the element (identity) in the init() of UserForm then it shows the Ul-Li correctly. But i wish to dynamicly add elements to SubForms from the RegisterForm.