Tag: Zend_Form

Zend Framework Form : Mastering Decorators

Hi dear reader,

today I am presenting you a workshop on how to master the Zend Framework form decorators.
I hope after taking a look at the code examples and explanations it all comes together and you will have a good understanding on how to use them.

Once you understand them you will start to love them, as it is a very easy way to write forms this way.

Sunny greetings and above all enjoy,

Nick Belhomme

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

Stand in front of a blackboard

Welcome once again my dearest reader.

Today I am gonna introduce you to the wonders of SlideShare
SlideShare is the world’s largest community for sharing presentations & slideshows. You can upload your PowerPoint, OpenOffice, Keynote or PDF files, tag them, embed them into your blog or website, browse others’ presentations, and comment on individual slides.

It offers endless possibilities. You ever wanted to give presentations, seminars, stand in front of a class or just put whatever you know online? Well it can.

And because the community is so big, you can find lots and lots of easy to read and follow presentations. The other day one of my dear friends Michelangelo van Dam put a slideshow online giving an introduction to a great feature of Zend Framework: Zend_Form.

It goes in depth on how to automatically create a registration, login and reset password form by the use of one xml file. I know I will be using this feature on my upcoming website. The flexibility and the ease of implementation had me stunned.

If you haven’t clicked through to the slide, I suggest you do it now!

The Zend_Form Presentation

Did I mentioned it is free?!

And do not forget you can contact me at any time if you think you have something good to share.

Sunny greetings from Belgium,
Nick Belhomme