summaryrefslogtreecommitdiff
path: root/framework/Web/UI/WebControls/TReCaptchaValidator.php
diff options
context:
space:
mode:
authorctrlaltca@gmail.com <>2011-12-19 13:20:38 +0000
committerctrlaltca@gmail.com <>2011-12-19 13:20:38 +0000
commite542ea49ceb77ad48f144a9f4ab1eaaafe4dc198 (patch)
treefcb0547f1128b705b828a59d4ac8f7c9222bbe02 /framework/Web/UI/WebControls/TReCaptchaValidator.php
parentc35b6041631a11aa7ba822d03b3ad13794db17f9 (diff)
committed patch from #378
Diffstat (limited to 'framework/Web/UI/WebControls/TReCaptchaValidator.php')
-rw-r--r--framework/Web/UI/WebControls/TReCaptchaValidator.php63
1 files changed, 57 insertions, 6 deletions
diff --git a/framework/Web/UI/WebControls/TReCaptchaValidator.php b/framework/Web/UI/WebControls/TReCaptchaValidator.php
index ae94ea59..2898a913 100644
--- a/framework/Web/UI/WebControls/TReCaptchaValidator.php
+++ b/framework/Web/UI/WebControls/TReCaptchaValidator.php
@@ -39,12 +39,29 @@ class TReCaptchaValidator extends TBaseValidator
*/
protected function getClientClassName()
{
- return '';
+ return 'Prado.WebUI.TReCaptchaValidator';
}
public function getEnableClientScript()
{
- return false;
+ return true;
+ }
+
+ protected function getCaptchaControl()
+ {
+ $control = $this->getValidationTarget();
+ if (!$control)
+ throw new Exception('No target control specified for TReCaptchaValidator');
+ if (!($control instanceof TReCaptcha))
+ throw new Exception('TReCaptchaValidator only works with TReCaptcha controls');
+ return $control;
+ }
+
+ public function getClientScriptOptions()
+ {
+ $options = parent::getClientScriptOptions();
+ $options['ResponseFieldName'] = $this->getCaptchaControl()->getResponseFieldName();
+ return $options;
}
/**
@@ -56,17 +73,51 @@ class TReCaptchaValidator extends TBaseValidator
*/
protected function evaluateIsValid()
{
- // check validity only once (if trying to evaluate multiple times, all redundant checks would fail)
+ // check validity only once (if trying to evaulate multiple times, all redundant checks would fail)
if (is_null($this->_isvalid))
{
- $control = $this->getValidationTarget();
- if(!($control instanceof TCaptcha))
- throw new TConfigurationException('recaptchavalidator_captchacontrol_invalid');
+ $control = $this->getCaptchaControl();
$this->_isvalid = $control->validate();
}
return ($this->_isvalid==true);
}
+ public function onPreRender($param)
+ {
+ parent::onPreRender($param);
+
+ $cs = $this->Page->getClientScript();
+
+ // communicate validation status to the client side
+ $value = $this->_isvalid===false ? '0' : '1';
+ $cs->registerHiddenField($this->getClientID().'_1',$value);
+
+ // check if we need to request a new captcha too
+ if ($this->Page->IsCallback)
+ {
+ // force update of validator display
+ if ($control = $this->getValidationTarget())
+ {
+ $cs->registerEndScript(
+ $this->getClientID().'::validate',
+ '$(\''.TJavaScript::quoteString($this->getClientID().'_1').'\').value = \''.TJavaScript::quoteString($value).'\';'.
+ 'Prado.Validation.validateControl(\''.TJavaScript::quoteString($control->ClientID).'\');'
+ );
+
+ if ($control->getVisible(true))
+ if ($this->_isvalid)
+ {
+ // if the challenge has been solved + we're in a callback and we still reach prerender phase,
+ // that means that some other validator failed and the user will be sent back to the page/form with
+ // the captcha control. in this case we need to force re-rendering of the control, because once
+ // solved, the old challenge won't validate anymore anyway
+
+ $control->regenerateToken();
+ }
+ }
+ }
+ }
+
}
?> \ No newline at end of file