N
N
nepster-web2013-11-30 21:14:04
Yii
nepster-web, 2013-11-30 21:14:04

Captcha in Yii without a model?

Faced such a problem, I use modal windows on the site and wrote a simple api in js for the following thing. For example, you need to accept something through confirmation and enter a captcha.
Let's say it will be a transaction between users. I suggest something, 2 the user must agree to the terms and confirm.
Actually, when you click on the button, a window opens in it with text and captcha. So this window can open when you click on any element with a certain class.
And in Yii, to open the captcha, you need to pass the model. I don't need to pass the model, how can I initialize the captcha without passing the model?

<div class="form_item">
        <label>Зищитный код:  <span class="necessarily">*</span></label>
        <span style="cursor: pointer;"><?php $this->widget('CCaptcha',  array('showRefreshButton'=>false, 'clickableImage'=>true)); ?></span>
        <div class="clear"></div>
        <div class="inputVerifyCode"><?php echo CHtml::activeTextField($model,'verifyCode', array('placeholder'=>'Введите код','name'=>'verifyCode')); ?></div>
        <div class="error"></div>
    </div>

$model - I just need to freeze this item.
Can you tell me if this can be done?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
_
_ _, 2013-12-02
@nepster-web

The meaning of captcha is to check its validity when performing some actions.
How will you do it without a model?
You are aware that a model in Yii means not only CActiveRecord, but everything that is inherited from CModel, for example, CFormModel?
So you can do like this:

class AgreementForm extends CFormModel{
      var $captchaText;
      public function rules(){
          return array(
              array('captchaText', 'captcha')
          );
      }
}

// Код контроллера
public function actionAgreement(){
      $form = new AgreementForm();
      $form->attributes = $_POST['AgreementForm'];
      $form->validate();
      if($form->hasErrors()){
      // No way, you've got missed something, dude
      }else{
      // Hooray         
      }
}

E
Eugene, 2013-11-30
@Nc_Soft

chtml::textfield

N
nepster-web, 2013-12-01
@nepster-web

And what to do with the absence of this:

<script type="text/javascript">
/*<![CDATA[*/
jQuery(function($) {

jQuery(document).on('click', '#yw0', function(){
    jQuery.ajax({
        url: "\/my\/path\/captcha?refresh=1",
        dataType: 'json',
        cache: false,
        success: function(data) {
            jQuery('#yw0').attr('src', data['url']);
            jQuery('body').data('captcha.hash', [data['hash1'], data['hash2']]);
        }
    });
    return false;
});

});
/*]]>*/
</script>

N
nepster-web, 2013-12-02
@nepster-web

there was a moment not that I check it without a model, I didn’t need to pass the model in the field that creates the input, but in the controller (for a previously unknown one) I just have to get the post itself with captcha.
For api confirmation windows. For example, if I want to do some kind of financial operation. For example, a user buys something on the site, he needs to show the user agreement and captcha. With this, I have a model that simply displays a list of services on the screen and does not validate anything. And confirmation throws the user to a completely different controller that works with completely different models. I will not pull the model of another module into the module and only for the sake of captcha.
Therefore, I generally moved away from this option. I found a topic where they complained about the rather weak documentation on YII, examples were given there that are not in the documentation, and I found just the captcha option for myself.
Now the captcha is always in 1 action, and I took this js code to my js file, where I can finally manage the code change. Everything went very well without any hamalays.
I'll try your suggestion, thanks!

D
densol7, 2021-05-26
@densol7

<script type="text/javascript">
/*<![CDATA[*/
jQuery(function($) {

jQuery(document).on('click', '#yw0', function(){
    jQuery.ajax({
        url: "\/my\/path\/captcha?refresh=1",
        dataType: 'json',
        cache: false,
        success: function(data) {
            jQuery('#yw0').attr('src', data['url']);
            jQuery('body').data('captcha.hash', [data['hash1'], data['hash2']]);
        }
    });
    return false;
});

});
/*]]>*/
</script>

Tell me the source of the captcha, I want to add it to my site

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question