Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-30-2008, 06:58 AM
Junior Member
 
Join Date: May 2008
Posts: 2
Thumbs up Implementing Dynamic Text Box in Zend_Form

Hie i am trying to find out how i can implement dynamic text boxes in Zend, so what i want to do is if someone selects the number of children it will generate text boxes equaling the number of children selected in the below Select :
So i am not sure how i can achieve this with Zend_From

<?php
$number_of_children = new Zend_Form_Element_Select('number_of_children');
$number_of_children->setLabel('Number of your children below the ags of 21')
->setMultiOptions(array('1', '2', '3', '4', '5' ,'6' ,'7', '8'))
->setRequired(true)->addValidator('NotEmpty', true);
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-21-2008, 02:39 AM
Member
 
Join Date: Jun 2008
Location: Florida
Posts: 88
Default

Here is the general idea of what I'd do...

Note that I added an option to the select box that submits the form via javascript. This way you can submit the form when the user changes the value of this form control, and you can check in the controller logic to see if the form was submitted via post, and if it was submitted via the submit button or not.

Code:
    // number_of_children
    $number_of_children = new Zend_Form_Element_Select('number_of_children');
    $number_of_children->setLabel('Number of your children below the ags of 21')
                       ->setOptions(array('onChange' => 'submit();'))
                       ->setMultiOptions(array('1', '2', '3', '4', '5' ,'6' ,'7', '8'))
                       ->setRequired(true)
                       ->addValidator('NotEmpty', true); 
    
    // text fields for a name for each child
    if ($this->_request->isPost()) {
        // if number_of_children submitted the form, and not the save submit        
        // button then use its value to create name fields
        if ($this->_request->getPost('save') != 'save' && !empty($this->_request->getPost('number_of_children'))) {
            for ($i = 0; $i < $this->_request->getPost('number_of_children'); $i++) {
                // define the field name for this iteration.  the first one will be
                // named child_name_1, then child_name_2, and so on...
                // and add any options, validators, filters, etc...
                $fieldName = new Zend_Form_Element_Text('child_name_' . $i);
                $fieldName->setLabel('Name:')
                          ->setOptions()
                          ->addValidator()
                          ->setRequired()
                          ->addFilter();            
            }
        }
    }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 03:49 AM.