*/
// Load Zend Framework
set_include_path(implode(PATH_SEPARATOR, array(get_include_path(), __DIR__ . '/')));
// Create the auto loader so zend can load the rest automatically
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
// load custom namspace
// in ZF MVC it would be autoloaderNamespaces[] = RM
$autoloader->registerNamespace('RM');
$form = new Zend_Form();
// Set form name, id, method and action
$form->setName('contact')->setAttrib('id', 'form-contact')->setMethod('post')->setAction('http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
// Create text box for name
$name = new Zend_Form_Element_Text('name');
$name->setRequired(TRUE)->setAttrib('id', 'fullname')->addErrorMessage('Please provide your name');
$ssn = new RM_Form_Element_Ssn('ssn');
$ssn->setRequired(TRUE)->setAttrib('id', 'ssn')->addErrorMessage('Please provide a valid SSN');
// Create text box for email
$email = new Zend_Form_Element_Text('email');
$email->setRequired(TRUE)->setAttrib('id', 'email')->addErrorMessage('Please provide a valid e-mail address');
// Create text area for message
$message = new Zend_Form_Element_Textarea('message');
$message->setRequired(TRUE)->setAttrib('id', 'message')->addErrorMessage('Please specify a message');
// Create a submit button
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Send Your Message!')->setAttrib('id', 'submit');
// Set the ViewScript decorator for the form and tell it which
// template file to use
$form->setDecorators(array(array('ViewScript', array('viewScript' => 'form-contact.tpl.php'))));
// Add the form elements AFTER the viewscript decorator has been set
$form->addElements(array($name, $ssn, $email, $message, $submit));
Example #2
0
// Load Zend Framework
set_include_path(implode(PATH_SEPARATOR, array(get_include_path(), __DIR__ . '/')));
// Create the auto loader so zend can load the rest automatically
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
// load custom namspace
// in ZF MVC it would be autoloaderNamespaces[] = RM
$autoloader->registerNamespace('RM');
$form = new Zend_Form();
// Set form name, id, method and action
$form->setName('contact')->setAttrib('id', 'form-contact')->setMethod('post')->setAction('http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
// Create text box for name
$name = new Zend_Form_Element_Text('name');
$name->setRequired(TRUE)->setAttrib('id', 'fullname')->addErrorMessage('Please provide your name');
// Create a custom composite element for SSn
$ssn = new RM_Form_Element_Ssn('ssn');
$ssn->setRequired(TRUE)->setAttrib('id', 'ssn')->addValidator(new Zend_Validate_NotEmpty())->addValidator(new RM_Validate_SpecialSsn());
// Using addErrorMessage orvrrides all error messages, including the one
// returned by RM_Validate_SpecialSsn.
// The custom element does not set the values if it is not a valid SSN.
// In those cases, it actually failes the NotEmpty validation.
// I am setting a error message specificly for those cases. This way there
// is no conflict with other other validation messages
$ssn->getValidator('NotEmpty')->setMessage('Please provide a valid SSN');
// Create text box for email
$email = new Zend_Form_Element_Text('email');
$email->setRequired(TRUE)->setAttrib('id', 'email')->addErrorMessage('Please provide a valid e-mail address');
// Create text area for message
$message = new Zend_Form_Element_Textarea('message');
$message->setRequired(TRUE)->setAttrib('id', 'message')->addErrorMessage('Please specify a message');
// Create a submit button