Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
systems:yii2:email_tools [2018/05/10 16:49]
smayr [Setup Captcha]
systems:yii2:email_tools [2018/05/14 11:08] (current)
smayr [Setup Captcha]
Line 146: Line 146:
                 <?= $form->field($model, 'subject') ?>                 <?= $form->field($model, 'subject') ?>
                 <?= $form->field($model, 'body')->textArea(['rows' => 6]) ?>                 <?= $form->field($model, 'body')->textArea(['rows' => 6]) ?>
-                <!-- // To regenerate new captcha after each refresh, call getVerifyCode().  +                 
-                     // See: http://www.yiiframework.com/forum/index.php/topic/17638-captcha-code-not-changing +                <?php  
-                 --> +                    // To regenerate new captcha after each refresh, call getVerifyCode().  
-                <?php $this->context->createAction('captcha')->getVerifyCode(true) ?>+                    // See: http://www.yiiframework.com/forum/index.php/topic/17638-captcha-code-not-changing 
 +                    $this->context->createAction('captcha')->getVerifyCode(true)
 +                ?>
                 <?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [                 <?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
                     'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',                     'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
Line 442: Line 444:
  
 == Setup Captcha == == Setup Captcha ==
 +
 +''CaptchaAction'' requires either GD2 extension or ImageMagick PHP extension. Check that it meets this requirement.
  
 In controller's function ''behaviors()'', add ''captcha'' to ''actions'' for user group that requires the use ot if: In controller's function ''behaviors()'', add ''captcha'' to ''actions'' for user group that requires the use ot if:
Line 506: Line 510:
 } }
 </code> </code>
 +
 +In model using the captcha, add ''verifyCode'' entry to the ''rules()'' function:
 +<code php>
 +/**
 + * ContactForm is the model behind the contact form.
 + */
 +class ContactForm extends Model
 +{
 +    public $name;
 +    public $email;
 +    public $subject;
 +    public $body;
 +    public $verifyCode;
 +
 +    /**
 +     * @return array the validation rules.
 +     */
 +    public function rules()
 +    {
 +        return [
 +            // name, email, subject and body are required
 +            [['name', 'email', 'subject', 'body'], 'required'],
 +            // email has to be a valid email address
 +            ['email', 'email'],
 +            // verifyCode needs to be entered correctly
 +            ['verifyCode', 'captcha', 'captchaAction' => 'site/captcha'],
 +        ];
 +    }
 +    
 +    //...
 +}
 +</code>
 +
 +In form view using the captcha, add ''verifyCode'' field:
 +<code php>
 +<?php 
 +   // To regenerate new captcha after each refresh, call getVerifyCode(). 
 +   // See: http://www.yiiframework.com/forum/index.php/topic/17638-captcha-code-not-changing/.
 +   $this->context->createAction('captcha')->getVerifyCode(true); 
 +?>
 +<?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
 +     'captchaAction' => 'site/captcha',  // redirect to correct controller where captcha is defined
 +     'template'      => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
 +]) ?>
 +</code>
 +
 +See more:
 +  * [[https://www.yiiframework.com/doc/api/2.0/yii-captcha-captchaaction|Yii CaptchaAction]]