Darrin's Tech Ramblings

Zend Framework Forms


Zend Forms … what can you do with it?

The general structure of a Zend_Form class is :

class my_form extends Zend_Form
    protected $_originalData; //holds the data used to populate the form.

     * setOriginalData is automatically used by the
     * constructor when invoked with the argument...
     *           (array('originalData' => $array_of_data));
    public function setOriginalData(array $original_data)
        $this->_originalData = $original_data;
        return $this;

    public function init()
        if (empty($this->_originalData)) {
            throw new Zend_Form_Exception("originalData must be set during construction.");

             ->setAttrib('enctype', 'multipart/form-data');

        // initialize form

        // --------------- csrf handling --------------------------
        $this->addElement(new Zend_Form_Element_Hash('csrf',
                                                     array('salt'    => $this->getName(),
                                                           'timeout' => 1800)));

             ->setMessage('There was a problem with your form submission, please try again.');

        // ---------------------------------------------------------
        $street = new Zend_Form_Element_Text('map_street');
        $street ->setOptions(array('size' => '40', 'maxlength' => '60'))
                ->setLabel("Street address");

        $city = new Zend_Form_Element_Text('map_city');
        $city ->setOptions(array('size' => '40', 'maxlength' => '40'))

        $submit = new Zend_Form_Element_Submit('submit');
        $submit ->setOptions(array('ignore' => true))
                ->setAttrib('class','bp') // add a class of 'bp' to the input element

        $cancel = new Zend_Form_Element_Button('cancel');
        $cancel ->setOptions(array('ignore' => true))
	        // add a class of 'bs' to the button element
                // suppress the ability to 'tab' to the cancel button
                ->setLabel('Cancel and exit to other.php')
                ->setAttrib('onclick', "window.location.href='/other.php';return false");

        // setup the form elements

        // add data to the form...


The point of this blog is to highlight the things you can do with a form element all in one place (i.e. here).

->setLabel("Element Label")


->setAtrtrib('class','<class_name>') // associate a class name with the element
->setAttrib('tabindex','-1') // suppress tabbing to this element

->setOptions(array('size'      => '20,'
                   'maxlength' => '40') // for text elements
->setOptions(array('rows'      => '2',
                   'cols'      => '30',
                   'maxlength' => 200,
                   'style'     => 'width:100%')) // for textarea elements

->addDecorator('HtmlTag', array('tag'=>'div',

               array('messages'=> "Please enter a value, it's reqired."))
                     'messages'=> "Please enter a number with up to 6 decimals."))
               array('min' => -90,
                     'max' => 90,
                     'messages'=> Please enter a Latitude between -90 and 90."));
               array('messages' => "zip code must be all numeric."))
               array(3, 5, 'messages'=>"value must be 3 to 5 characters long."));
               array(5, 5, 'messages'=>"zip code must be 5 digits."));

NOTE: NUMBER_6DECIMAL = /^-?[0-9]*(\.[0-9]{1,6})?$/