Here are the view helpers I created to do my dirty work:
Code: Select all
<?php
/**
* Class returns an error message if there is an error for the particular key passed in
* If no key is passed in, it checks to see if any of the form's elements have an error
* otherwise it just returns false
*/
class Zend_View_Helper_formError
{
/**
* The view object that created this helper object.
* @var Zend_View
*/
public $view;
public function formError($name = null)
{
// this next line is here so that if formError() is called without argument,
// it will return false if there are no errors at all
if (is_null($name)) return empty($this->view->errorMessages);
// if the errorMessage array has not been created, create it.
if (!is_array($this->view->errorMessages)) $this->view->errorMessages = array();
// return the error message(s) associated with the $named element
return (array_key_exists($name, $this->view->errorMessages)) ? $this->view->errorMessages[$name] : false;
}
}
/**
* This class allows for the controller to set an error for a particular element
* in my form
*/
class Zend_View_Helper_formSetError
{
/**
* The view object that created this helper object.
* @var Zend_View
*/
public $view;
public function formSetError($name, $error)
{
// if the error messages array has not been created, create it
if (!is_array($this->view->errorMessages)) $this->view->errorMessages = array();
// if the array of messages for this particular element has not been created, create it
if (!array_key_exists($name, $this->view->errorMessages)) $this->view->errorMessages[$name] = array();
// set the error for this element
$this->view->errorMessages[$name][] = $error;
}
}
/**
* Class returns the value for a specific form element if there is one,
* otherwise it returns a blank string
*/
class Zend_View_Helper_formValue
{
public function formValue($name = null, $get = null)
{
$front = Zend_Controller_Front::getInstance();
$data = (is_null($get)) ? $front->getRequest()->getPost() : $front->getRequest()->getQuery();
return (array_key_exists($name, $data)) ? $data[$name] : '';
}
}Code: Select all
<form method="post" action="<?php echo $this->url(array('action' => 'create')); ?>">
<fieldset id="vendor-info">
<legend>Vendor Creation</legend>
<?php if ($this->formError()): ?><p class="error">Your form contains some errors. Please check below and fix any errors you see.</p><?php endif; ?>
<div class="meta">
<p class="header">Vendor Creation</p>
<p>Please enter the necessary data to the left. Required elements are denoted by bold text.</p>
</div>
<dl>
<dt class="required"><label for="vendor_name">Vendor Name</label></dt>
<dd class="<?php if ($this->formError('vendor_name')) echo 'error' ?>">
<div class="message">
<?php if ($this->formError('vendor_name')) echo '<p>' . $this->formError('vendor_name') . '</p>'; ?>
<?php echo $this->formText('vendor_name', $this->formValue('vendor_name')); ?>
</div>
</dd>
<dt><label for="corporate_name">Corporate Name</label></dt>
<dd class="<?php if ($this->formError('corporate_name')) echo 'error' ?>">
<div class="message">
<?php if ($this->formError('corporate_name')) echo '<p>' . $this->formError('vendor_name') . '</p>'; ?>
<?php echo $this->formText('corporate_name', $this->formValue('corporate_name')); ?>
</div>
</dd>
</dl>
<div class="submit clear" style="clear:left"><input type="submit" value="Submit"></div>
<div class="clear"></div>
</fieldset> <!-- /user-info -->
</form>