yii2 验证码总是报错
请教大家一个问题,我的yii2在使用的时候总是报错。代码如下
模型
<?php
namespace app\models;
use Yii;
use yii\mongodb\ActiveRecord;
use yii\captcha\captcha;
/**
* DSP用户模型
*/
class UsersMongo extends ActiveRecord
{
public $verifyCode;
/**
* [getDb 设置链接的数据库]
* @return [type] [description]
*/
public static function getDb()
{
return Yii::$app->mongo_dsp;
}
/**
* [collectionName 集合名称]
* @return [type] [description]
*/
public static function collectionName()
{
// return 'dsp_users';
return 'demo_users';
}
/**
* [scenarios 场景设置--不同业务逻辑下不同的验证条件]
* @return [type] [description]
*/
public function scenarios()
{
return [
'DoLogin' => ['email','password','verifyCode'], //登录时
'DoRegister' => ['real_name','email','company_name','phone','password','verifyCode'], //注册时
];
}
/**
* [rules 验证规则]
* @return [type] [description]
*/
public function rules()
{
$real_name_msg = '用户名称应由2~20个字符组成';
$company_name_msg = '公司称名应由2~20个字符组成';
$phone_msg = '手机号码格式有误';
$password_msg = '密码长度应在6~16个字符之间';
return [
[['real_name','email','company_name','phone','password','verifyCode'],'required','message'=>'{attribute}不能为空','on'=>'DoLogin'],
[['email','password','verifyCode'],'required','message'=>'{attribute}不能为空','on'=>'DoRegister'],
//用户名
['real_name','string','length'=>[2,20],'tooShort'=>$real_name_msg,'tooLong'=>$real_name_msg],
//邮箱
['email','email','message'=>'邮箱格式错误'],
//公司名称
['company_name','string','length'=>[2,50],'tooShort'=>$company_name_msg,'tooLong'=>$company_name_msg],
//手机号码
['phone','match','pattern'=>'/^1[3|4|5|8][0-9]\d{4,8}$/','message'=>$phone_msg],
//密码
['password','string','length'=>[6,16],'tooShort'=>$password_msg,'tooLong'=>$password_msg],
//确认密码
['password2', 'compare', 'compareAttribute' => 'password','message'=>'两次输入密码必须一致'],
['verifyCode','captcha','message'=>'验证码有误', 'captchaAction'=>'users/captcha']
];
}
控制器
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\models\UsersMongo;
use app\models\TestMongo;
use yiii\AccessControl;
use yii\Session;
/**
* DSP用户控制器
*/
class UsersController extends Controller
{
public $layout = 'login_layout';
public function actions()
{
return [
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'height' => 50,
'width' => 100,
'minLength' => 4,
'maxLength' => 4,
],
];
}
public function actionTest()
{
$session = Yii::$app->session;
return $this->renderpartial('test',['model'=>new UsersMongo]);
}
public function actionDoLogin()
{
session_start();
$model = new UsersMongo;
$model->setScenario('DoLogin'); //设置本次模型使用什么场景
$data = Yii::$app->request->post();
$session = Yii::$app->session;
var_dump($_SESSION);
if($model->load($data) && $model->validate()){
echo json_encode($data);
}else{
$errors = $model->getErrors();
var_dump($errors);
}
}
视图
<script type="text/javascript" src="<?php echo Yii::$app->request->baseUrl;?>/javascript/jquery.js"></script>
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\widgets\Pjax;
use yii\captcha\captcha;
Pjax::begin(['id'=>'demo']);
$form = ActiveForm::begin([
'id' => 'login-form',
'options' => ['class' => 'form-horizontal','data-pjax' => 'demo'],
'action' => ['users/do-login'],
'method' => 'post',
'ajaxDataType' => 'json'
]) ?>
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<?= $form->field($model,'verifyCode')->widget(yii\captcha\Captcha::className(),['captchaAction'=>'users/captcha','imageOptions'=>['alt'=>'点击换图','title'=>'点击换图', 'style'=>'cursor:pointer']]);?>
<div class="form-group">
<div class="col-lg-offset-1 col-lg-11">
<?= Html::submitButton('Login', ['class' => 'btn btn-primary']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
<?php Pjax::end(); ?>
<?php
$this->registerJs(
'$(document).ready(function(){
$("#new_country").on("pjax:end", function() {
$.pjax.reload({container:"#countries"}); //Reload GridView
});
});'
);
?>
<script type="text/javascript">
$(document).ready(function(){
$("#usersmongo-verifycode-image").click(function(){
var _captcha = $(this);
$.getJSON("/user/captcha?refresh=true",function(res){
var dataUrl = _captcha.attr("dataUrl");
if(!dataUrl){
dataUrl = _captcha.attr("src");
_captcha.attr("dataUrl",dataUrl);
}
_captcha.attr("src",dataUrl+"?rd="+Math.random());
});
});
});
</script>
报错结果
请大神指点
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你看看这个https://www.yiichina.com/topi... 能不能帮到你,不行再告诉我。