/ Published in: PHP
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
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
class Application_Form_Abstract extends Zend_Form { // customizing default decorators ... function loadDefaultDecorators() { if ($this->loadDefaultDecoratorsIsDisabled()) { return $this; } // ... for elements $decorators = $this->_elementDecorators; 'ViewHelper', 'Errors', 'Label', )); $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(); $this->addDecorator('FormElements') ->addDecorator(new Application_Form_Decorator_FormMessages) ->addDecorator('Form'); } return $this; } // set values mainly used to set values from the request params function setValues($values) { foreach ($this->getElements() as $elem) { $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 'label' => 'Humans, please DO NOT fill this up', new Application_Validator_HoneyPot ), 'ViewHelper', 'Errors', 'Label', ) )); // 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') { } function getMessages() { return $this->_messages; } } // honey pot validator class Application_Validator_HoneyPot extends Zend_Validate_Abstract { const BOT = 'bot'; 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(); 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 .= '</div>'; return $html; } }