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 17 comments
Take into consideration, that you should not add your recaptcha field to required validator, as this will fail.
Also, keep in mind, that adding following line:
below your recaptcha field works only for server-side validation. Even, if you have client-side validation enabled, it will not work this way.
Extension works like a charm, so many thanks to the creator.
If you decide to replace default verifyCode with this extension, remember to remove some then unecessary elements from your application:
CCaptchaActioninactions()function in controller responsible for handling your previous bulid-in captcha,verifyCoderule fromrules()from your form model as well as public symbol$verifyCodeform it,Remember, that you can customize look & feel of your captcha -- look into documentation for more info.
Just want to say thank you for this extension. Good work!
P.S. If someone knows how to make this work with ajax validation it would be good to share with the rest of us.
The form was successfully submitting as if the reCaptcha was not there at all. Even if it was left blank. I had to set enableAjaxValidation to FALSE and replaced the ajax validation code in my controller action, with the $model->validate() to get it work.
THANKS A LOT!!!!!!!
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.