Want more features on Pastebin? Sign Up, it's FREE!
Guest

Jeremy Kendall

By: a guest on Jul 23rd, 2010  |  syntax: PHP  |  size: 2.68 KB  |  views: 369  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. class IndexController extends Zend_Controller_Action
  4. {
  5.  
  6.     public function indexAction()
  7.     {
  8.        
  9.         /**
  10.          * $interests and $referral are the arrays that will be used for the
  11.          * interests checkboxes and referral select element.
  12.          */
  13.         $interests = array('Hiking', 'Bowhunting', 'Ninjitsu', 'Ted Nugent');
  14.         $referral = array('Friend', 'Interwebs', 'TV', 'Radio', 'Other');
  15.        
  16.         $form = new Application_Form_Example();
  17.        
  18.         /**
  19.          * Setting the post method is not strictly necessary.  Zend_Form
  20.          * uses the post method by default
  21.          */
  22.         $form->setMethod('post');
  23.        
  24.         /**
  25.          * Here's where I'm setting the options for the interests checkboxes and
  26.          * the referral select element.
  27.          *
  28.          * Notice that I'm using Zend_Form's fluid interface to add the
  29.          * Zend_Validate_InArray validator to each element
  30.          *
  31.          * The use of array_merge and array_combine is one of my tricks and tips
  32.          * from my Zend_Form_Element_Multi - Tips and Tricks blog post (http://bit.ly/bEZl37)
  33.          */
  34.         $form->getElement('interests')
  35.             ->addMultiOptions(array_combine($interests, $interests))
  36.             ->addValidator(new Zend_Validate_InArray($interests));
  37.        
  38.         $form->getElement('referral')
  39.             ->addMultiOptions(array_merge(array('Please make a selection . . .'), array_combine($referral, $referral)))
  40.             ->addValidator(new Zend_Validate_InArray($referral));
  41.        
  42.            
  43.         /**
  44.          * If the request is not a post request, then the form hasn't been
  45.          * submitted and should be displayed.
  46.          *
  47.          * $this->view->form = $form set the form property of the Zend_View
  48.          * object to our example form object for retrieval in the view
  49.          */
  50.         if (!$this->getRequest()->isPost()) {
  51.             $this->view->form = $form;
  52.             return;
  53.         /**
  54.          * Request is post, but if the form doesn't pass validation,
  55.          * it should be displayed again with current field values and
  56.          * error messages
  57.          */
  58.         } elseif (!$form->isValid($_POST)) {
  59.             $this->view->form = $form;
  60.             return;
  61.         }
  62.        
  63.         /**
  64.          * The form has passed validation and the validated and filtered form values
  65.          * are ready for use/manipulation/processing.
  66.          *
  67.          * For the purposes of this example, the validated & filtered form values
  68.          * are being dumped to the view.
  69.          */
  70.         Zend_Debug::dump($form->getValues(), 'Form Values');
  71.     }
  72.  
  73. }
clone this paste RAW Paste Data