Yii Captcha错误

时间:2013-05-18 19:10:08

标签: php yii validation captcha

我在Yii工作,我只是一个初学者,并尽我所能学习框架,这里是我被困在的地方:

我已经创建了一个用户模型和所需的表单,我正在尝试为它实现Captcha:

这是我在用户模型中的验证规则:

$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中的覆盖动作():

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库,如果我导航到网站/联系人,那么验证码就会生成。

我似乎不明白,我在哪里弄错了。

这是我看到的事情:

enter image description here

这些表格似乎工作正常但是,我无法看到验证码图像。

任何帮助都将不胜感激。

此致

1 个答案:

答案 0 :(得分:1)

我得到了答案,这是因为控制器中定义的访问规则,我不得不像这样修改控制器accessControl:

public function accessRules()
    {
        return array(
            array('allow',  // allow all users to perform 'index' and 'view' actions
                'actions'=>array('index','view','captcha'),
                'users'=>array('*'),
            ),
            array('allow', // allow authenticated user to perform every action
                'actions'=>array('create','update','admin','delete'),
                'users'=>array('@'),
            ),

            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }