EReCaptchaValidator is a validator which uses reCAPTCHA to validate a CAPTCHA rendered by the EReCaptcha widget.
You need to have valid public and private keys for this to work. You can get them by registering yourself at http://recaptcha.net/
Current version: 1.3
protected/extensionsIn the view:
echo CHtml::activeLabel($user, 'validacion'); <?php $this->widget('application.extensions.recaptcha.EReCaptcha', array('model'=>$user, 'attribute'=>'validacion', 'theme'=>'red', 'language'=>'es_ES', 'publicKey'=>'<your public key>')) <?php echo CHtml::error($user, 'validacion');
In the model:
class UserModel extends CFormModel { public $validacion; public function rules() { return array( array('validacion', 'application.extensions.recaptcha.EReCaptchaValidator', 'privateKey'=>'<your private key>'), ); } public function attributeLabels() { return array( 'validacion'=>Yii::t('demo', 'Enter both words separated by a space: '), ); } }
imehesz has noted that for this validator to work with ActiveRecord, you'll need to do something like this:
Controller:
public function actionRegister() { $form = new User(); $form->scenario = 'registerwcaptcha'; ... if($form->validate()) { // and here is the actual HACKY part $form->scenario = NULL; // save user registration $form->save(); } }
Model:
public function rules() { return array( ... array( 'validacion', 'application.extensions.recaptcha.EReCaptchaValidator', 'privateKey'=> ENVII_CAPTCHA_PRIVATE_KEY, 'on' => 'registerwcaptcha' ), ... ); }
The view remains the same.
Total 12 comments
I suggest to update setLanguage() in EReCaptcha.php to this:
Then you can put your custom language or set it dynamically by application. And update setTheme() method too to put your custom theme.
Thank you.
thanks, very helpful.
I use this extension for a project, a website which runs over SSL.
I know that reCaptcha has support for HTTPS requests so users will not get warnings in browser (like "this page has some resources that are not sent via a secured connection") when loading a page that has reCaptcha.
This is how I got this done:
In EReCaptcha.php, I modified the line ~153 as follows:
You do not need to change the first argument Yii::app()->params['recaptcha_public'] to use ReCaptcha via HTTPS. I did this just to avoid hardcoding it in my application. See this comment.
In configuration, protected/config/main.php, I added
And, finally, I updated the reCAPTCHA/recaptchalib.php file. The latest one can be downloaded from here.
Still learning here. I hardcoded the reCaptcha keys in the code with my previous example, but it's better to follow the tips in these comments too: http://www.yiiframework.com/extension/recaptcha/#c1350 http://www.yiiframework.com/extension/recaptcha/#c1383
That is, put the keys in the config. Mind the case sensitivity as the two linked comments have different cases.
If you want to use yii-user with recaptcha, you'll need to do the following (these are all relative to yii-user module root, NOT app root):
in views/user/registration.php, replace:
with
in models/RegistrationForm.php, replace the verifyCode rule with this:
and very important:
In controllers/RegistrationController.php, around line 45, find this line:
and change it to:
Here's the reason the last bit is important: The call to the reCaptcha server is only valid ONCE. However, this controller ends up calling it twice, once at line 34 ($model->validate()) and then again at line 45 ($model->save()) because the save() actually calls the validate() again.
Passing false as a parameter to save() will bypass the validation, which is okay because we just did it a few lines earlier.
Hope this saves someone a few hours. :)
There is an extraneous comma after {$this->tabIndex}, <---Please remove it from code since this will cause the theme to not work in IE.
$script =<<theme}', custom_theme_widget : {$customthemewidget}, lang : '{$this->language}', tabindex : {$this->tabIndex} }; EOP;
I had a little hick-up at the beginning, but now it works great. I will use this everywhere ;)
--iM
~So1
in order to do so just rewrote two lines in both php files of this extension and replace $this->public/private with Yii::app()->params['reCaptcha']['publicKey'] and Yii::app()->params['reCaptcha']['privateKey'].
It works like a charm now.
Thanks for the extension. I'd like to use it in a little bit different way. I do not want to type keys manually in each recaptcha widget, instead of this I define keys in config:
Thank you!
Nice interface to recaptcha
Leave a comment
Please login to leave your comment.