【发布时间】:2013-05-18 19:10:07
【问题描述】:
我在 Yii 工作,我只是一个初学者,正在努力学习这个框架,这就是我陷入困境的地方:
我已经创建了一个用户模型和所需的表单,我正在尝试为它实现验证码:
这是我在用户模型中的验证规则:
$public verifyCode
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('username, password, email', 'required'),
array('username','unique'),
array('email','email'),
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
array('username, password', 'length', 'max'=>45),
array('email', 'length', 'max'=>100),
array('active', 'length', 'max'=>1),
array('created_on, updated_on', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, username, password, email, created_on, updated_on, active', 'safe', 'on'=>'search'),
);
}
这是我在 userController 中覆盖的 action() :
public function actions(){
return array(
'captcha'=>array(
'class' => 'CCaptchaAction',
)
);
}
这是我的视图文件:
<?php if(CCaptcha::checkRequirements()): ?>
<div class="row">
<?php echo $form->labelEx($model,'verifyCode'); ?>
<div>
<?php $this->widget('CCaptcha'); ?>
<?php echo $form->textField($model,'verifyCode'); ?>
</div>
<div class="hint">Please enter the letters as they are shown in the image above.
<br/>Letters are not case-sensitive.</div>
<?php echo $form->error($model,'verifyCode'); ?>
</div>
<?php endif; ?>
据我说,我认为我做的一切都是正确的,但是验证码图像没有生成。哦,是的,GD 库已安装,如果我导航到站点/联系人,则验证码生成良好。
我好像不明白,我哪里弄错了。
这是我看到的:
表单似乎工作正常,但是我看不到验证码图像。
任何帮助将不胜感激。
问候,
【问题讨论】:
-
CCaptchaAction问题可能有访问权限。你看过this solution吗? -
同时检查 php 中是否有
gd库。尝试直接获取图片,看看有没有错误,将图片下载到电脑上查看内容,也可以有一些提示。 -
gd 没问题,因为
CCapcha::checkRequirements()返回true -
我得到了答案,因为我没有修改我的控制器代码。
-
修改我的控制器 accessRules 但验证码图像未显示后
标签: php yii validation captcha