Friday, April 25, 2008

PHP Icons in Amsterdam

This Monday I received an invitation from iBuilding's CTO Ivo Jansch to come down to Amsterdam to have dinner with Zend's Chief Editor Cal Evans, symfony gury and iBuilding's consultant Stefan Koopmanschap and Zend developer Remi Woler.

Besides having a great dinner at a typical Amsterdam diner Moeders (thanks Ivo!), I enjoyed the good conversations with all and totally by surprise Cal had not forgotten Stefan and I once asked him for a Zend water cooler. We both have now a signed copy of Cal's book "php|architect's Guide to Programming with Zend Framework". Thank you Cal.

Afterwards I surprised Cal and Ivo with a mild Cuban cigar while we were heading back to the hotel both Cal and Ivo stayed.

After a late night drink I took Remi with me to drop him off at his house (a little d-tour to Gouda) before I arrived back home in the arms of my wife who was by then already fast asleep.

Thursday, April 03, 2008

Login and authentication with Zend Framework

As part of my tutorial, I don't want to exclude the magnificent "Login and Authentication with Zend Framework" how to by Matthew Weier O’Phinney.

Although it's not using a configuration file to render the form, it's a magnificent mini tutorial on how you should implement Zend_Form for authentication purposes, hence all kinds of purposes.

Quoting Cal Evans on Zend Developer Zone: "If you are working with Zend Framework then this is a must read tutorial.". I agree completely !

Wednesday, April 02, 2008

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
Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 License.