<?php
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
/**
* $interests and $referral are the arrays that will be used for the
* interests checkboxes and referral select element.
*/
$interests = array('Hiking', 'Bowhunting', 'Ninjitsu', 'Ted Nugent');
$referral = array('Friend', 'Interwebs', 'TV', 'Radio', 'Other');
$form = new Application_Form_Example();
/**
* Setting the post method is not strictly necessary. Zend_Form
* uses the post method by default
*/
$form->setMethod('post');
/**
* Here's where I'm setting the options for the interests checkboxes and
* the referral select element.
*
* Notice that I'm using Zend_Form's fluid interface to add the
* Zend_Validate_InArray validator to each element
*
* The use of array_merge and array_combine is one of my tricks and tips
* from my Zend_Form_Element_Multi - Tips and Tricks blog post (http://bit.ly/bEZl37)
*/
$form->getElement('interests')
->addMultiOptions(array_combine($interests, $interests))
->addValidator(new Zend_Validate_InArray($interests));
$form->getElement('referral')
->addMultiOptions(array_merge(array('Please make a selection . . .'), array_combine($referral, $referral)))
->addValidator(new Zend_Validate_InArray($referral));
/**
* If the request is not a post request, then the form hasn't been
* submitted and should be displayed.
*
* $this->view->form = $form set the form property of the Zend_View
* object to our example form object for retrieval in the view
*/
if (!$this->getRequest()->isPost()) {
$this->view->form = $form;
return;
/**
* Request is post, but if the form doesn't pass validation,
* it should be displayed again with current field values and
* error messages
*/
} elseif (!$form->isValid($_POST)) {
$this->view->form = $form;
return;
}
/**
* The form has passed validation and the validated and filtered form values
* are ready for use/manipulation/processing.
*
* For the purposes of this example, the validated & filtered form values
* are being dumped to the view.
*/
Zend_Debug::dump($form->getValues(), 'Form Values');
}
}