I want to force my site to refresh the captcha image everytime it loads, so I have a javascript method triggered with the onload() event. Here I have the following line:
document.getElementById('yw0_button').click;
Firebug doesn't detect any errors, and for testing purposes I have added an alert right after the displayed line, and the alert pops up each time the page is loaded. However, the image doesn't refresh!
This is what I believe is relevant about the view file:
<?php if(extension_loaded('gd')): ?>
<div class="row">
<?php echo $form->labelEx($model,'verifyCode'); ?>
<div>
<?php
$this->widget('CCaptcha',
array('showRefreshButton'=>true,
'buttonType'=>'button',
'buttonOptions'=>
array('type'=>'image',
'src'=>"/path/images/refresh-icon.png",
'width'=>30,
),
'buttonLabel'=>'Refrescar imagen'),
false);
?>
<br>
<?php echo $form->textField($model,'verifyCode'); ?>
</div>
<div class="hint">
Porfavor ingrese las letras como las ve en la imagen superior.
<br/>No hay distincion entre minúsculas y mayúsculas.</div>
</div>
<?php endif; ?>
Any ideas?
@k to the z just saw this! Yes sure, if you could help me find a more proper solution it would be awesome! This is what i believe is relevant about the view file:
<?php if(extension_loaded('gd')): ?>
<div class="row">
<?php echo $form->labelEx($model,'verifyCode'); ?>
<div>
<?php
$this->widget('CCaptcha',
array('showRefreshButton'=>true,
'buttonType'=>'button',
'buttonOptions'=>
array('type'=>'image',
'src'=>"/path/images/refresh-icon.png",
'width'=>30,
),
'buttonLabel'=>'Refrescar imagen'),
false);
?>
<br>
<?php echo $form->textField($model,'verifyCode'); ?>
</div>
<div class="hint">
Porfavor ingrese las letras como las ve en la imagen superior.
<br/>No hay distincion entre minúsculas y mayúsculas.</div>
</div>
<?php endif; ?>
In the controller I grant authorized users permission in the accessRules() method to the captcha action, and thats about all. Is there something else I could post?
I hope I can propose a better way to solve the problem mentioned by Soph. I know this is not JavaScript-related solution this question is addicted to but as I can see this one is the most right way to go.
I faced the same requirement - to refresh the caption on page refresh but preserve this one when user's input is invalid on other fields because repeating a new verification code on each validation's fail is annoying. So this task looks like to not refresh the caption on
POST
request and refresh it otherwise (GET
request). A solution proposed by Soph by himself can't achieve this - as one could knowPOST
requests are sent to server and their data cannot be analyzed by client-side JavaScript.Therefore I decided to look at Yii's captcha implementation. It consists of two classes located in
system.web.widgets.captcha
package. The former isCCaptcha
extended fromCWidget
and that we use in our view to add a captcha to web form in the manner like this:The latter is
CCaptchaAction
that does the most significant work to provide a captcha by generating a verification code and creating an image. Taking into account that Yii is designed to be object-oriented we can create a couple of our own classesCaptcha
andCaptchaAction
extended fromCCaptcha
andCCaptchaAction
relatively and place them incomponents
subdirectory of our web application's directory.My implementation of both these classes is below.
run()
method ofCCaptchaAction
is overriden and is quite similar to original one excepting that there is one additionalif
-branch executing whenrender_refreshed
GET-parameter is set and where new verification code is being generated on the fly and corresponding new image is being rendered and returned as action's result.A public
refresh
variable has been introduced inCaptcha
class and defaults tofalse
meaning widget's behavior is similar toCCaptcha
's one. OverridingrenderImage
method we alter a code responsible for preparing widget's output HTML code. To be more precisive it's where a link to captcha action is prepared used assrc
attribute of animg
tag. In the case ofrefresh
member is set totrue
an additionalrender_refreshed
parameter is appended to this link.Here is
CaptchaAction.php
:And this is
Captcha.php
:So the usage is quite simple. In action preparing model's data for site's page do the following:
After that modify captcha's widget call in
myView
template of this page's action:and don't forget to modify
actions
method of page's controller by replacingCCaptchaAction
class byCaptchaAction
:Seems to me that it works as expected. Hope this will help somebody.
I have a better solution for you guys:
Put these codes into the controller which you include captcha action!