Darrin's Tech Ramblings

Zend Framework Forms

Advertisements

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.");
        }

        $this->setName('MapLocationForm')
             ->setAttrib('enctype', 'multipart/form-data');

        // initialize form
        $this->setMethod('post');

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

        $this->getElement('csrf')
             ->getValidator('Identical')
             ->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'))
                ->setRequired(false)
                ->addFilter('StringTrim')
                ->setLabel("Street address");

        $city = new Zend_Form_Element_Text('map_city');
        $city ->setOptions(array('size' => '40', 'maxlength' => '40'))
              ->setRequired(false)
              ->addFilter('StringTrim')
              ->setLabel("City");

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

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

        // setup the form elements
        $this->addElement($street)
             ->addElement($city)
             ->addElement($submit)
             ->addElement($cancel);

        // add data to the form...
        $this->populate($this->_originalData);
    }

}

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")

->setRequired(true|false)

->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',
                                'class'=>'input-wrapper'))

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

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