Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Jeremy Kendall

By: a guest on Jul 23rd, 2010  |  syntax: PHP  |  size: 3.96 KB  |  views: 357  |  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. /**
  4.  * All of this configuration, from error reporting down to instantiating
  5.  * the Zend_View object, would usually be in another file and included
  6.  * here with require_once.  For the purposes of this example, I've included
  7.  * everything here.
  8.  */
  9. error_reporting(-1);
  10. ini_set('display_errors', 1);
  11.  
  12. /**
  13.  * Remember, the entire Zend Framework library should be included
  14.  * in this project.  Please be sure to change the include path
  15.  * to reflect your environment.
  16.  */
  17. $includePath = array(
  18.   get_include_path(),
  19.   '/home/jkendall/phplib/Zend/library',
  20. );
  21. set_include_path(implode(PATH_SEPARATOR, $includePath));
  22.  
  23. date_default_timezone_set('America/Chicago');
  24.  
  25. require_once 'Zend/Loader/Autoloader.php';
  26. $autoloader = Zend_Loader_Autoloader::getInstance();
  27.  
  28. $view = new Zend_View();
  29.  
  30. /**
  31.  * Remember to change this include path to match your environment!
  32.  */
  33. require_once dirname(dirname(__FILE__)) . '/introZendForm/application/forms/Example.php';
  34.  
  35. ?>
  36.  
  37. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  38. <html>
  39.     <head>
  40.         <title>A Brief Introduction to Zend_Form</title>
  41.         <link href="/~jkendall/introZendForm/public/css/zend_form.css" media="screen" rel="stylesheet" type="text/css" />
  42.     </head>
  43.     <body>
  44.    
  45.     <h1>A Brief Introduction to Zend Form</h1>
  46.        
  47.     <h2>Example Registration Form - Standalone</h2>
  48.  
  49. <?php
  50.  
  51.         /**
  52.          * $interests and $referral are the arrays that will be used for the
  53.          * interests checkboxes and referral select element.
  54.          */
  55.         $interests = array('Hiking', 'Bowhunting', 'Ninjitsu', 'Ted Nugent');
  56.         $referral = array('Friend', 'Interwebs', 'TV', 'Radio', 'Other');
  57.        
  58.         $form = new Application_Form_Example();
  59.        
  60.         /**
  61.          * Setting the post method is not strictly necessary.  Zend_Form
  62.          * uses the post method by default
  63.          */
  64.         $form->setMethod('post');
  65.        
  66.         /**
  67.          * Here's where I'm setting the options for the interests checkboxes and
  68.          * the referral select element.
  69.          *
  70.          * Notice that I'm using Zend_Form's fluid interface to add the
  71.          * Zend_Validate_InArray validator to each element
  72.          *
  73.          * The use of array_merge and array_combine is one of my tricks and tips
  74.          * from my Zend_Form_Element_Multi - Tips and Tricks blog post (http://bit.ly/bEZl37)
  75.          */
  76.         $form->getElement('interests')
  77.             ->addMultiOptions(array_combine($interests, $interests))
  78.             ->addValidator(new Zend_Validate_InArray($interests));
  79.        
  80.         $form->getElement('referral')
  81.             ->addMultiOptions(array_merge(array('Please make a selection . . .'), array_combine($referral, $referral)))
  82.             ->addValidator(new Zend_Validate_InArray($referral));
  83.  
  84.         /**
  85.          * If the request is a get request, then the form hasn't been
  86.          * submitted and should be displayed.
  87.          */
  88.         if (strtolower($_SERVER['REQUEST_METHOD']) == 'get') {
  89.             // Render form
  90.             echo $form->render($view);
  91.         } else {
  92.             /**
  93.              * If the form doesn't pass validation, it should be displayed
  94.              * again with current field values and error messages
  95.              */
  96.             if (!$form->isValid($_POST)) {
  97.                 // Failed validation - render form with errors
  98.                 echo $form->render($view);
  99.             } else {
  100.                 /**
  101.                  * The form has passed validation and the validated and filtered form values
  102.                  * are ready for use/manipulation/processing.
  103.                  *
  104.                  * For the purposes of this example, the validated & filtered form values
  105.                  * are being dumped to the view.
  106.                  */
  107.                 Zend_Debug::dump($form->getValues());
  108.             }
  109.         }
  110.  
  111. ?>
  112.  
  113. </body>
  114. </html>
clone this paste RAW Paste Data