« Return to Thread: Zend_Form : Subforms, dynamic element creation and best practices

Re: Zend_Form : Subforms, dynamic element creation and best practices

by maxarbos :: Rate this Message:

Reply to Author | View in Thread


Matthew Weier O'Phinney-3 wrote:
>
> The TestingForm.php is built as such:
> TestForm extends Zend_Form {
>    public function __construct($options=null) {
>           parent::__construct($options);
>
>      $this->setAttrib('accept-charset', 'UTF-8');
> }

I'd place the above in setAttrib() call as the first call in your init()
method below, and get rid of __construct() entirely.

>    public function init() {
>      $name = $this->addElement('text', 'name', array().....);
> }
>
> }
>
>
I left out that we are also including :

        $this->setDecorators(array(
            'FormElements',
            array('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form')),
            array('Description', array('placement' => 'prepend')),
            'Form'
        ));

in the __construct, but moving this all to the init() doesnt seem to load the decorators in time.


Matthew Weier O'Phinney-3 wrote:
> 2)  I want to be able to add elements dynamically to the form depending on a
> result set of data from the db.
>
> Say a person has 4 children, I want to dynamically add four text elements to
> the form, one for each record retrieved from the db.
>
> How can I do this with my setup?

Yes. Create an element for each child, and attach each with a different
form element name:

    foreach ($children as $key => $child) {
        $form->addElement('text', 'child' . $key, array('value' => $child'));
    }
I was pretty much doing the same things, but included
foreach($children as $key=>$val){
$field.$key = $this->addElement('text', 'fieldname'.$key, etc....)

$group[]='fieldname'.$key;
}

        $this->addDisplayGroup(
            $number_group, 'groupname' ....)


and ended up getting error of:

'No valid elements specified for display group'


So once I got rid of the '$field.$key ='   it all worked correctly.


Do you have any other updated resources for best practices or structure of a large site using this framework?

thanks.

 « Return to Thread: Zend_Form : Subforms, dynamic element creation and best practices