Why can't the Yii2 captcha pass the Client side check?

I use the Yii2 administration module, the captcha image is displayed on the form. After entering the confirmation code, the tips always displayed an error message on the client side, but I am sure that it was entered correctly. Then I look at the source code, I found that I did not set the attribute captchaAction( \yii\captcha\CaptchaValidator) correctly, the default value is equal , but my controller , I think the captchaAction value should be , but how to set it? captchaActionsite/captchaapp\modules\admin\controllers\PublicControlleradmin/public/captcha

Below is the code for my view page:

<?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
    'template' => '<div class="row"><div class="col-lg-4">{image}</div> &nbsp;<div class="col-lg-7">{input}</div></div>',
    'captchaAction' => 'public/captcha',
 ]); ?>

Here is my controller:

public function actions()
{
    return [
        'error' => [
            'class' => 'yii\web\ErrorAction',
        ],
        'captcha' => [
            'class' => 'yii\captcha\CaptchaAction',
            'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            'minLength' => 3,
            'maxLength' => 5,
        ],
    ];
}
public function actionLogin()
{
    $model = new LoginForm();
    if ( Yii::$app->request->isPost ) {
        # code ...
    } else {
        return $this->render('login', [
            'model' => $model,
            'title' => Yii::$app->params['adminLogin'],
        ]);
    }
}
+4
source share
5 answers

, AJAX. Yii , Ajax. , :

  • ajaxValidation, clientValidation. :
$form = ActiveForm::begin([
    'id'                     => 'registration-form',
    'enableAjaxValidation'   => true,
    'enableClientValidation' => false
]);
  1. CaptchaAction, getVerifyCode AJAX.
if(\Yii::$app->request->isAjax == false) {
    $this->getVerifyCode(true);
}
+2

captcha SiteController 3 :

  • captchaAction , captcha:

    function rules()
    {
        return [
            ...
            ["verificationCode", "captcha", 'captchaAction' => 'your-controller/captcha']
        ];
    }
    
  • captcha :

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }
    
  • :

    <?= $form->field($model,
        "verificationCode")->widget( Captcha::className(), [
            'captchaAction' => 'your-controller/captcha'
        ]); ?>
    
+2

.

enableAjaxValidation , .

<?php $form = ActiveForm::begin([
    'id' => 'some-id',
    'enableAjaxValidation' => true,
    'enableClientValidation' => false,
]); ?>

, , . enableAjaxValidation . - Captcha false.

, AJAX, , , . , , AJAX , .

<?= $form->field($model, 'username', ['enableAjaxValidation' => true]) ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<?= $form->field($model, 'verifyCode')->widget(Captcha::className()) ?>

, AJAX , , , .


:

, @AnHuy answer.

captcha (SiteController Yii2) actions.

    public function actions() {
        return [
            // ...
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
            // ...
        ];
    }

yii\captcha\CaptchaAction , Yii, app\models\captcha\CaptchaAction.

    public function actions() {
        return [
            // ...
            'captcha' => [
                'class' => 'app\models\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
            // ...
        ];
    }

, models/captcha/CaptchaAction.php.

<?php

namespace app\models\captcha;

use Yii;
use yii\captcha\CaptchaAction as CaptchaActionBase;

class CaptchaAction extends CaptchaActionBase {
    public function validate($input, $caseSensitive) {
        // Skip validation on AJAX requests, as it expires the captcha.
        if (Yii::$app->request->isAjax) {
            return true;
        }
        return parent::validate($input, $caseSensitive);
    }
}

, AJAX, true , . , .

+1

   ['verifyCode', 'captcha','captchaAction'=>'public/captcha' ],
0

, "captcha" .

public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::className(),
            'rules' => [
                [
                    'actions' => ['captcha', (whatever ...)],
                    'allow' => true,
                ],

....

0

All Articles