0
votes

Whan I add 'on' => 'create' in my rule my captcha always incorrect Off that work good why? I try to let it to other controller support/captcha but still incorrect

MyView

 <?= Captcha::widget([
                        'id' => 'captcha',
                        'model' => $model,
                        'attribute' => 'verifyCode',
                        'options' => ['class' => 'form-control',
                            'data-v-rule' => '',
                            'captchaAction' => 'support/captcha',
                            'data-v-msg' => $i18n->t('please.input.verify.code'),
                            'placeholder' => $i18n->t('please.input.verify.code', '', false)]
                    ]); ?>

Myrule

...

        [['verifyCode'], 'captcha', 'message' => $i18n->t('error.verifyCode'),'captchaAction' => 'support/captcha' , 'on' => 'create'],
...

I get seesion

$_SESSION = [
    '__flash' => [],
    '__captcha/site/captcha' => 'nnbioo',
    '__captcha/site/captchacount' => 1,
    '__captcha/support/captcha' => 'cacijq',
    '__captcha/support/captchacount' => 1,
];

Mycontroller

public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => CaptchaAction::className(),
            ],
        ];
    }
1
I had get verifyCode in post - user7189675
But loss in model it say have no attribute why~~???? - user7189675
I had public $verifyCode in my model - user7189675
solve by this way $customerServiceModel->verifyCode = $post['CustomerServiceModel']['verifyCode']; - user7189675
I don't know why model load verifyCode null - user7189675

1 Answers

0
votes

$customerServiceModel->load($post) is not working, because attribute verifyCode is not safe in this case. Your rule is specified with 'on' => 'create' - so it means, it's save on scenario create. You didn't assigned this scenario to the model so there's 2 solutions:

  1. Remove 'on' => 'create' from rule
  2. Assign create scenario to model by $customerServiceModel->scenario = 'create'; before using load().