Tutorial on using Zend_Form with Zend_Config_Xml and selectboxes

Using config files is great for creating rapid forms, but sometimes you need something extra like select boxes filled with data (e.g. states, countries, ...). How are you going to implement this ?

Well, I created a view helper to fetch those common listings from my database when I was using pre 1.5 Zend Framework form elements.

My /application/default/views/helpers/getListing.php file:
<?php
/**
* View helper class to retrieve common listings from
* the database

*
* @author Michelangelo van Dam
*
*/
class Zend_View_Helper_GetListing {
// our database adapter

protected
$_dbAdapter;

// property to access the "view" part of MVC

protected
$_view;

/**

* we initiate the view and our database adapter
*
* @param Zend_View_Interface $view

*/

public function setView(Zend_View_Interface $view) {
$this->_view = $view; $this->_dbAdapter = Zend_Registry::get('db');
}


/**
* Let's fetch our listing from the database
* @param string $listing
* @return array

*/
public function getListing($listing) {
switch ($listing) {
case
'countries':

$countries = $this->_dbAdapter->fetchAll("SELECT * FROM `sc_countries`");
if
(count($countries) > 0) {

$data = array(0 => '-- Select a country --');
foreach ($countries as $country) {
$data[$country['country_code']] = $country['country']; }
}
}
break;
case 'states':
$states = $this->_dbAdapter->fetchAll("SELECT * FROM `sc_states`");
if
(count($states) > 0) {

$data = array(0 => '-- select a state --', 'OT' => 'Outside the USA');
foreach ($states as $state) {
$data[$state['state_code']] = $state['state'];
}
}
break;
default
:

$data = array();
break;
}

return $data;
}
}

Now that we have a view helper, we need to call this in our controller. To continue my previous tutorial, I will use the same UserController.php and userforms.xml.

In our userforms.xml we need to add a few fields in our registration form configuration. I just going to add the states and countries here. I assume you can add other elements there without problem now to retrieve the address, zip and location information. I also skip any validation and filtering here, to keep things readable.
...
<register>

...
<state>
<type>select</type>
<required>
true</required>

<options>

<label>Select your state:</label>
</options>
</state>

<country>

<type>select</type>
<required>
true</required>

<options>
<label>
Select your country:</label>

</options>

</country>

</register>
...

In our UserController.php we have:
public function registerAction() {
$this->view->form = $this->getForm('register');
}

We're going to change this with:
public function registerAction() {
$form = $this->getForm('register');
// I created a default route to register
$form->setAction($this->view->url(array(), 'register'));

// we populate the state select box
$stateElement = $form->getElement('state');
$stateElement->setMultiOptions($this->view->getListing('states'); // we populate the country select box
$countryElement = $form->getElement('country');
$countryElement
->setMultiOptions($this->view->getListing('countries'); }

The result:



see alo my Tutorial on using Zend_Form with Zend_Config_Xml
Blogged with the Flock Browser

Comments

  1. Anonymous4/4/08 10:28

    You have Syntax error

    $country['country']; }
    }
    }

    should be

    $country['country'];
    }
    }

    anyway, thanks :)

    ReplyDelete
  2. Tnx for the heads-up. Blogspot is not really easy to use for displaying code. Fix is implemented.

    ReplyDelete
  3. Hello!

    I don't speak English.

    you can send me your exemple "Tutorial on using Zend_Form with Zend_Config_Xml and selectboxes" in .ZIP

    thanks.

    ReplyDelete
  4. Eden, if you can hang in there, I'm finishing up these tutorial sessions including selectboxes, translations and more. Just watch my blog here...

    ReplyDelete
  5. Can you UN-Encode an encoded script that was encoded using "Zend Encoder"?

    ReplyDelete

Post a Comment

Popular Posts