|
|||
|
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); ?> |
|
|||
|
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();
}
}
}
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|