yii2: captcha is incorrect
messageBoard model
<?php
namespace frontend\models;
use Yii;
/**
* This is the model class for table "message_board".
*
* @property integer $id
* @property string $firstname
* @property string $lastname
* @property string $telephone
* @property string $email
* @property string $content
* @property string $date_added
*/
class MessageBoard extends \yii\db\ActiveRecord
{
public $verifyCode;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'message_board';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['firstname', 'lastname', 'email', 'content'], 'required'],
//[['date_added'], 'safe'],
[['firstname', 'lastname', 'email', 'content'], 'string', 'max' => 255],
[['telephone'], 'string', 'max' => 10],
// verifyCode needs to be entered correctly
['verifyCode', 'captcha']
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'firstname' => 'Firstname',
'lastname' => 'Lastname',
'telephone' => 'Telephone',
'email' => 'Email',
'content' => 'Content',
'date_added' => 'Date Added',
'verifyCode' => 'Verification Code',
];
}
//get all message in the board
public function getMessages(){
return static::find()->all();
}
}
sitecontroller, the problem is on the actionContact
<?php
namespace frontend\controllers;
use Yii;
use common\models\LoginForm;
use frontend\models\PasswordResetRequestForm;
use frontend\models\ResetPasswordForm;
use frontend\models\SignupForm;
use frontend\models\ContactForm;
use frontend\models\MessageBoard;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
/**
* Site controller
*/
class SiteController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout', 'signup'],
'rules' => [
[
'actions' => ['signup'],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
/**
* @inheritdoc
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
// 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
public function actionIndex()
{
return $this->render('index');
}
public function actionContact()
{
$model = new MessageBoard();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->save();
return $this->refresh();
} else {
return $this->render('contact', [
'model' => $model,
]);
}
}
}
captcha file: vendor\yiii2soft\yii2\captcha\CaptchaAction.php
when the code run to $model->validate(), the captcha verify return true

but after that, in the process of $model->save(), the captcha is changed. and validate return false.

then I cant save data because the captcha is always incorrect
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 20 (8 by maintainers)
Hi again, it turned out to be related to that https://github.com/yiisoft/yii2/commit/c8c6882fc4ef8de11d1d5fa402b4303a06ab4079 , i had a call to validate() before the save() and thatโs what changed the captcha code.
There is actually a documentation about that here http://stuff.cebe.cc/yii2docs/yii-captcha-captchavalidator.html โNote that once CAPTCHA validation succeeds, a new CAPTCHA will be generated automatically. As a result, CAPTCHA validation should not be used in AJAX validation mode because it may fail the validation even if a user enters the same code as shown in the CAPTCHA image which is actually different from the latest CAPTCHA code.โ maybe it should be with H1 tags ๐
By calling save() with false parameter e.g. $model->save(false); resolved the issue.
As i see, @shyandsy code also has a call to validate(), so iโm guessing that using save(false) will resolve his issue as well, hopefully.
I apologize if i wasted Your time ๐
If someone can now help me with that question http://www.yiiframework.com/forum/index.php/topic/59588-captcha-custom-template-how-to-get-error-into-the-widgets-template/ it would be great ๐
Again Thanks .