<?php
/**
* All of this configuration, from error reporting down to instantiating
* the Zend_View object, would usually be in another file and included
* here with require_once. For the purposes of this example, I've included
* everything here.
*/
error_reporting(-1);
ini_set('display_errors', 1);
/**
* Remember, the entire Zend Framework library should be included
* in this project. Please be sure to change the include path
* to reflect your environment.
*/
$includePath = array(
get_include_path(),
'/home/jkendall/phplib/Zend/library',
);
set_include_path(implode(PATH_SEPARATOR, $includePath));
date_default_timezone_set('America/Chicago');
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
$view = new Zend_View();
/**
* Remember to change this include path to match your environment!
*/
require_once dirname(dirname(__FILE__)) . '/introZendForm/application/forms/Example.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>A Brief Introduction to Zend_Form</title>
<link href="/~jkendall/introZendForm/public/css/zend_form.css" media="screen" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>A Brief Introduction to Zend Form</h1>
<h2>Example Registration Form - Standalone</h2>
<?php
/**
* $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 a get request, then the form hasn't been
* submitted and should be displayed.
*/
if (strtolower($_SERVER['REQUEST_METHOD']) == 'get') {
// Render form
echo $form->render($view);
} else {
/**
* If the form doesn't pass validation, it should be displayed
* again with current field values and error messages
*/
if (!$form->isValid($_POST)) {
// Failed validation - render form with errors
echo $form->render($view);
} else {
/**
* 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());
}
}
?>
</body>
</html>