Yii - 如何在CPortlet中显示显示验证码?

时间:2012-08-13 12:24:05

标签: yii

我正在使用portlet来显示联系表单 - 与标准生成的静态页面上的内容完全相同。我使用的是相同的ContactForm模型。表单显示但验证码未显示并报告“CCaptchaValidator.action”验证码“无效。无法在当前控制器中找到此类操作。”

我是yii的新手所以需要花费大量的时间来解决所有问题。我有什么快速的建议来做它的工作吗?

最佳!

Yii::import('zii.widgets.CPortlet');

class ContactFormCard extends CPortlet
{
    public $title='Contact';

protected function renderContent()
{
    $model=new ContactForm;
    if(isset($_POST['ContactForm']))
    {
        $model->attributes=$_POST['ContactForm'];
        if($model->validate())
            $this->controller->refresh();
    }
    $this->render('contactFormCard',array('model'=>$model));
}
public function actions()
{
    return array(
        'captcha'=>array(
            'class'=>'CCaptchaAction',
            'backColor'=>0xFFFFFF,
        ),
    );
}

}

并在portlet视图中:

<?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; ?>

1 个答案:

答案 0 :(得分:2)

在SiteController.php中添加:

    public function actions() {
        return array(
            'captcha' => array(
                'class' => 'CCaptchaAction',
            'backColor' => 0xFFFFFF,
            ),

            // [...]
        );
    }

在你的ContactForm.php中添加:

public function rules() {
    return array(
        // [...]

        array('verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements(), 'message' => Yii::t('formsErros', 'Código de verificação incorreto.')),

        // [...]
    );
}

在你的view.php

<!-- [...] -->

<?php if(CCaptcha::checkRequirements()): ?>
    <div class="captcha">
            <?=$form->labelEx($contactFormModel, 'verifyCode')?>
            <?php $this->widget('CCaptcha', array('clickableImage' => true, 'showRefreshButton' => true, 'imageOptions' => array('id' => 'captchaContactForm', 'class' => 'clickableCursor'), 'buttonLabel' => '')); ?>
            <?=$form->textField($contactFormModel, 'verifyCode', array('class' => 'verticalAlignBottom'))?> 
            <?=$form->error($contactFormModel, 'verifyCode')?>
    </div>
<?php endif; ?>

<!-- [...] -->
相关问题