Sunday, July 5, 2009

select box in Zend framework: code and example

Though I’ve already discuss how to create dropdown in one of my previous article, however here I would discuss it a bit in detail.

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.

9 comments:

  1. 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

    $this->addElement($country)

    ReplyDelete
  2. 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).

    thx in advance.

    ReplyDelete
  3. Company Logo Design
    Hey… 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.

    ReplyDelete
  4. what is tabel name for "countries", in your database ..?

    ReplyDelete
    Replies
    1. protected $_name = 'countries'; => table named "countries" ofcourse

      Delete
  5. What 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

    ReplyDelete
  6. hi ,

    $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

    ReplyDelete
  7. Hi,

    good 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

    ReplyDelete