Skip to content

Instantly share code, notes, and snippets.

@cgsmith
Created March 31, 2015 13:02
Show Gist options
  • Save cgsmith/13eb2b7b107011fc493b to your computer and use it in GitHub Desktop.
Save cgsmith/13eb2b7b107011fc493b to your computer and use it in GitHub Desktop.
<?php
namespace Cgsmith\Form\Element;
/**
* Class Recaptcha
* Renders a div for google recaptcha to allow for use of the Google Recaptcha API
*
* @package Cgsmith
* @license MIT
* @author Chris Smith
*/
class Recaptcha extends Zend_Form_Element
{
/** @var string specify formRecaptcha helper */
public $helper = 'formRecaptcha';
/** @var string siteKey for Google Recaptcha */
protected $_siteKey = '';
/** @var string secretKey for Google Recaptcha */
protected $_secretKey = '';
/**
* Constructor for element and adds validator
*
* @param array|string|Zend_Config $spec
* @param null $options
* @throws Zend_Exception
* @throws Zend_Form_Exception
*/
public function __construct($spec, $options = null)
{
if (empty($options['siteKey']) || empty($options['secretKey'])) {
throw new Zend_Exception('Site key and secret key must be specified.');
}
$this->_siteKey = trim($options['siteKey']); // trim the white space if there is any just to be sure
$this->_secretKey = trim($options['secretKey']); // trim the white space if there is any just to be sure
$this->addValidator('Recaptcha', false, ['secretKey' => $this->_secretKey]);
$this->setAllowEmpty(false);
parent::__construct($spec, $options);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment