'Leave Empty', 'name' => 'whyDoYouLoveWinter', ); public function init() { $this->_initSpamProtection(); return parent::init(); } public function validate($data = null) { if (!$this->_validateSpamProtection()) { return false; } return parent::validate($data); } protected function _initSpamProtection() { if ($this->_hasSpamProtection) { if (self::SPAM_PROTECTION_COOKIE == $this->_spamProtectionType) { $this->_initCookieProtection(); } else { $this->_initEmptyFieldProtection(); } } return $this; } protected function _initCookieProtection() { $cookieUrl = Qs_SiteMap::findFirst(null, array('type' => 'FormCookie_'), null, 'url'); if (!$cookieUrl) { throw new Qs_Form_Exception('Page for cookie protection is not found!'); } $cookieUrl .= '?form=' . rawurlencode($this->getId()); /** @var Qs_Doc $doc */ $doc = Zend_Registry::get('doc'); $doc->addStylesheet($cookieUrl, array('skipPacking' => true)); return $this; } protected function _initEmptyFieldProtection() { $field = $this->_spamProtectionField; $this->addElement('text', $field['name'], array('label' => $field['label'])); $element = $this->getElement($field['name']); if (($htmlTag = $element->getDecorator('HtmlTag'))) { /** @var Qs_Form_Decorator_HtmlTag $htmlTag */ $htmlTag->setOption('class', $field['name']); } return $this; } protected function _validateSpamProtection() { if ($this->_hasSpamProtection) { if (self::SPAM_PROTECTION_COOKIE == $this->_spamProtectionType) { $method = '_validateCookieProtection'; } else if (self::SPAM_PROTECTION_EMPTY_FIELD == $this->_spamProtectionType) { $method = '_validateEmptyFieldProtection'; } else { throw new Qs_Form_Exception('Unknown spam protections validation method'); } if (!$this->{$method}()) { $this->addError($this->_spamProtectionMessage); return false; } } return true; } protected function _validateCookieProtection() { if (!App_FormCookie_View::getCookie($this->getId())) { return false; } return true; } protected function _validateEmptyFieldProtection() { if ('' != Qs_Request::getRequestValue($this->_spamProtectionField['name'])) { return false; } return true; } public function setHasSpamProtection($hasSpamProtection) { $this->_hasSpamProtection = $hasSpamProtection; } }