Using Zend_Form, Zend_Config_Ini And Zend_Db_Table

Ok I am going to write a little bit about Zend_Form.
Yes I know a lot of small tutorials already exist but this one can
show you another trick 🙂

My prefered way of using Zend_Form is to use it with a config.ini file.
It gives you a nice overview of special settings in a structured way. I love that.

One thing the manual does not explain is how to use it in addition with for instance dynamic population/value setting of a Zend_Form_Element object.

Say you have a select field that needs population. You cannot do that manually in the config file every time the database gets updated. There should be a better way. And actually there is.

Small example stripped to the bare minimum follows:

formConfig.ini

 [time]

time.action = "/time/save"
time.method = "post"
time.accept-charset = "utf-8"

time.elements.deliverytime_id.type = "select"
time.elements.deliverytime_id.options.label = "Delivery Time: "

time.elements.createsubmit.type = "submit"
time.elements.createsubmit.options.label = "create new"

Above we have a regular config.ini file ready to be parsed with Zend_Config_Ini.
For more information on how to use this with Zend_Form see Using a Zend_Config object

Time.php

<?php
class Time extends Zend_Db_Table {
    protected $_name = 'time';
}

Time.php is the Model for our time Table. This way we can easily do table manipulation without writing
any sql. This for potential portability to another database and your convencience.

TimeController.php

<?php
class TimeController extends Zend_Controller_Action {
    protected $time;
    protected $formTime;
    
    public function indexAction() {
        $this->view->formTime = $this->formTime;
    }
    
    public function init() {
        $this->time = new Time();
        $formConfig = new Zend_Config_Ini(APP_CONFIG.'/formCms.ini', 'time');
        $this->formTime = new Zend_Form($formConfig->time);
        $this->setupDeliveryTimesInFormSelect();
    }
    
    private function setupDeliveryTimesInFormSelect() {
        $select = $this->formTime->getElement('deliverytime_id');
    
        $deliveryTimes = $this->Time->fetchAll()->toArray();
        foreach($deliveryTimes as $timeOption) {
            $select->addMultiOption($timeOption['id'], $timeOption['time']);
        }
    }
}

This is a regular Controller. This is just stripped example code just to make you understand everything with more clarity. The key is in setupDeliveryTimesInFormSelect(). Here we get the form element from our $this->formTime.  It was already generated in the init() function.
Because all things are static in the config file we are going to populate it with options using the addMultiOption(). The values that are being used in the foreach loop are returned by a Zend_Db_Table object generated in Time.php.

The result of this code if you render your index view is a form with a submit button and a select box with options/values pairs set straight from a database. Because it is a select,  the submitted values are compared to the values in the form. Other form element types like text etc should have their submitted values validated and or filtered. You can find plenty of examples on how to set those options in the config.ini file on the net.

Happy Coding

Nick Belhomme
Your PHP5 Zend Certified Engineer

One comment

  1. MatyCZ says:

    Hi,

    try this:

    $data = $this->PageModel->getControllers();

    $select = $pageForm->getElement(‘controller’);
    $select->setMultiOptions(data);

Leave a Reply

Your email address will not be published. Required fields are marked *