Revision: 29346
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at July 27, 2010 20:36 by jiewmeng
Initial Code
class Application_Form_Abstract extends Zend_Form {
protected $_messages = array();
// customizing default decorators ...
function loadDefaultDecorators() {
if ($this->loadDefaultDecoratorsIsDisabled()) {
return $this;
}
// ... for elements
$decorators = $this->_elementDecorators;
if (empty($decorators)) {
$this->setElementDecorators(array(
'ViewHelper',
'Errors',
array('Description', array('tag' => 'p')),
'Label',
array('HtmlTag', array('tag' => 'p'))
));
$this->getElement('submit')->removeDecorator('Label'); // remove the label from submit buttons
}
// ... for form
$this->addPrefixPath('Application_Form_Decorator', 'Application/Form/Decorator', 'decorator');
$decorators = $this->getDecorators();
if (empty($decorators)) {
$this->addDecorator('FormElements')
->addDecorator(new Application_Form_Decorator_FormMessages)
->addDecorator('Description', array('placement' => 'PREPEND', 'tag' => 'p'))
->addDecorator('Form');
}
return $this;
}
// set values mainly used to set values from the request params
function setValues($values) {
foreach ($this->getElements() as $elem) {
if (!$elem->getIgnore() && array_key_exists($elem->getName(), $values)) {
$elem->setValue($values[$elem->getName()]);
}
}
}
function __construct($options = null) {
// set default form method
$this->setMethod('post');
// add honey pot with a validator that basically gives an error if the input is filled
// hidden using css
$this->addElement('text', 'honeypot', array(
'label' => 'Humans, please DO NOT fill this up',
'validators' => array(
new Application_Validator_HoneyPot
),
'decorators' => array(
'ViewHelper',
'Errors',
array('Description', array('tag' => 'p')),
'Label',
array('HtmlTag', array('tag' => 'p', 'class' => 'honeypot', 'style' => 'display: none'))
)
));
// call parent constructor
parent::__construct($options);
}
// messenging functionality
// i actually tried to use Zend_Form's error messages but they includes error messages from its Zend_Form_Elements, not something i wanted
// so i created this to create messages specific to the form
// also messages have a $class param for css styling. eg. errors vs warning vs info
function addMessage($message, $class = 'info') {
$this->_messages[] = array('msg' => $message, 'class' => $class);
}
function getMessages() {
return $this->_messages;
}
}
// honey pot validator
class Application_Validator_HoneyPot extends Zend_Validate_Abstract {
const BOT = 'bot';
protected $_messageTemplates = array(
self::BOT => 'Bot detected! Please do not fill up the honey pot field!'
);
function isValid($value) {
if ($value != "") {
$this->_error(self::BOT);
return false;
}
return true;
}
}
// FormMessages decorator
class Application_Form_Decorator_FormMessages extends Zend_Form_Decorator_Abstract {
protected $_placement = 'PREPEND';
function render($content) {
$element = $this->getElement();
$view = $element->getView();
if (null === $view) {
return $content;
}
$messages = $element->getMessages();
if (empty($messages)) {
return $content;
}
$separator = $this->getSeparator();
$placement = $this->getPlacement();
$messagesStr = '';
foreach ($messages as $message) {
$messagesStr .= $this->renderMessage($message['msg'], $message['class']);
}
switch ($this->_placement) {
case self::APPEND:
return $content . $separator . $messagesStr;
case self::PREPEND:
return $messagesStr . $separator . $content;
}
}
function renderMessage($message, $class) {
$html = '<div class="formMsg formMsg-' . $class . '">';
$html .= strtoupper($class) . ': ' . $message;
$html .= '</div>';
return $html;
}
}
Initial URL
Initial Description
in this custom `Zend_Form`, i added the following customizations\r\n\r\n - customized default decorators\r\n - added functions to set values of inputs (mainly for request params)\\r\\n- added honey pot anti-bot measure (see `__construct()`)\r\n - messenging functionality - simply rendering out `div`s with a css class and the message inside\r\n\r\ncode includes \r\n - the custom form abstract class\r\n - honey pot validator\r\n - FormMessages decorator
Initial Title
Zend Form: Customized to include honeyot anti-bot measure and other functionality
Initial Tags
Initial Language
PHP