You can create it using the following code.
<?php
class CustomForm extends Zend_Form
{
public function init()
{
$this->setMethod('post');
$this->setAction('user/process');
$country = $this->createElement('select',’countries’);
$country ->setLabel('Countries:')
->addMultiOptions(array(
'US' => 'United States',
'UK' => 'United Kingdom'
));
}
}
Or can also be created using the following code.
<?php
class CustomForm extends Zend_Form
{
public function init()
{
$this->setMethod('post');
$this->setAction('user/process');
$country = new Zend_Form_Element_Select('countries');
$country ->setLabel('Countries:')
->addMultiOptions(array(
'US' => 'United States',
'UK' => 'United Kingdom'
));
}
}
The options are hard code in both of the cases. This will not be requirement always.
Sometime or most of the time application need that drop down should be populated fetching records from the data base. So for this purpose I would like to implement the following code.
Fist in your model write the following.
<?php
class Countries extends Zend_Table
{
protected $_name = 'countries';
public function getCountriesList()
{
$select = $this->_db->select()
->from($this->_name,
array('key' => 'id','value' => 'country_name'))
$result = $this->getAdapter()->fetchAll($select);
return $result;
}
}
Keep in mind that the records we fetch will have an associated array having id as key and country name as value. If you don’t defined the above array, array(‘key’ => ’id’,’value’ => ‘country_name’), as I have defined, you will not get what you want. So be careful while fetching records for constructing drop down.
And now in your form write the following code.
<?php
class CustomForm extends Zend_Form
{
public function init()
{
$this->setMethod('post');
$this->setAction('user/process');
$countries = new Countries();
$countries_list = $countires->getCountriesList();
$country = new Zend_Form_Element_Select('countries');
$country ->setLabel('Countries:')
->addMultiOptions( $countriesList
));
}
}
In the above code the only thing changes is
1. I have instantiated model using
$countries = new Countries();
2. Call getCountiresList() method. And assign the list to the instead of assign hard coded values.
hi, the example is very good but exist a problem in your form, your need add the $country to form, because dont display the Select in your form
ReplyDelete$this->addElement($country)
hi, could you pls explain how to generate dependent comboboxes(ie the options in one combo should be generated based on the selected value on the previous combo).
ReplyDeletethx in advance.
Company Logo Design
ReplyDeleteHey… I am pretty satisfied with the regard of working extra hours for us. I think the trade mark says it all. Your experts are really aware of market dynamics and understand the price of time in this environment.
oo
ReplyDeletewhat is tabel name for "countries", in your database ..?
ReplyDeleteprotected $_name = 'countries'; => table named "countries" ofcourse
DeleteWhat if I don't want to use it to retrieve but want to use it, as a way to call a funtion, from the specified model. so that when I select 1 from the addmultioption the fuction for one is called and excuted, 2 etc http://stackoverflow.com/questions/25666997/select-element-performing-a-function-on-submit-zend
ReplyDeletehi ,
ReplyDelete$type_Entry = new Zend_Form_Element_Select('type_Of_Entry');
$type_Entry->setLabel('Type Of Entry');
$type_Entry->addMultiOption('','Select Type Of Entry');
$type_Entry->addMultiOption('test1','test1');
$type_Entry->addMultiOption('test2','test2');
$type_Entry->setRequired(true);
$type_Entry->setRegisterInArrayValidator(false);
$type_Entry->addValidator('NotEmpty', false, array(
'messages' => 'Please select status.'
));
$this->addElements(array($type_Entry,$level_Intensity,$description,$submit));
this is my code bt i m not getting a selected value in my dropdown,
while i am posting the form it does not post a dropdown value
can any one please help me
Hi,
ReplyDeletegood information
Web application development services will successfully change the execution of your online business So, hire our skilled Ecommerce website company to get customized applications for your business.
Best Website Designing and Development Company
"
Ecommerce Website Development Company"
Zend Framework